After completing this chapter, you will be able to
Configure the FAST Search Center.
Create queries by using both Keyword Query Syntax and FQL.
Understand the different query APIs available and how to use them.
Go beyond the FAST Search Center and build truly flexible search applications.
Chapter 7, focused on how to get data into the search index. This chapter covers the different ways to get the data back out—in other words, how to query the index.
Included in Microsoft SharePoint are four different APIs that can be used when querying the search index, all catering to different usage scenarios. You can also use two different query languages: the Keyword Query Syntax is shared with SharePoint search, and the FAST Query Language is unique to Microsoft FAST Search Server 2010 for SharePoint (FS4SP). The latter is covered more in depth in this chapter.
Your first encounter with querying the search index usually happens when you execute a search query—either from the upper-right search box shown in Figure 8-1 or from the FAST Search Center shown in Figure 8-2.
The result page of the Search Center is composed of several Web Parts, which use APIs from the Federated Search Object Model to execute the search queries. Although it might seem likely that one of the search box Web Parts passes the query to the object model, the Search Core Results Web Part is actually what submits the query and displays the result set. The object model allows the result page to display results from both FS4SP and from People Search, in addition to other results such as news articles from the Internet—all from the same query. This process is called Federated Search and is explained in more detail in Chapter 2. It is essential that the query use this method because the People results come from a different search index.
In addition to searching with the Search Center, you can create custom Web Parts that take advantage of the APIs from the object model, or you can use the KeywordQuery class from the Query Object Model.
When the Federated Search Object Model returns results, it uses the Query Object Model internally to return results from FS4SP and People Search.
When developing applications hosted outside of SharePoint, you can take advantage of the Query Web Service, which makes it easy to execute searches against FS4SP from any application capable of communicating with a web service.
The query APIs are not unique to FS4SP—in fact, they’re the same as those used with SharePoint search. Having a common platform and common APIs for both FS4SP and SharePoint Server search makes it possible for developers to create search-based applications that work with both technologies. What is unique, however, are API features that let you take advantage of the more advanced query features available with FS4SP, such as dynamic ranking, sort by formula, deep refiners, and contextual search.
Another notable distinction with FS4SP is the support of the FAST Query Language (FQL). This is a powerful and rich query language, similar to T-SQL in many ways. In most cases, FQL is not suitable for end users, but you can take advantage of it in search-driven applications and use it in conjunction with your Search Center via search scopes.
This chapter discusses the different APIs available to developers, reviews the API capabilities, and provides typical use cases in which you might want to choose one API over the other.
Query languages are required to describe and pass the details of a user’s search to the search engine. Even when a user searches for only a single term, many additional parameters are usually added that add meaning and context to that search term and help to return a more relevant set of results. For example, if a user searches for invoice from the finance site in a SharePoint portal, the original search query will be appended with the user’s identity, a scope limiting results to items within the finance site, and any predefined search scopes that are set for the Search Center the user is searching from.
FS4SP uses two search query languages to describe and communicate search parameters: the Keyword Query Syntax built into SharePoint and the FAST Search specific FQL. Both query languages pass search terms (tokens) and operators (AND, OR, and so on). The terms are usually the text that a user has entered; operators are usually added to delimit or expand the relationships of those terms. Tokens can be free text query terms or specified properties.
The Keyword Query Syntax, sometimes referred to as the Keyword Query Language (KQL), is a query syntax that works with both SharePoint search and FS4SP. KQL was originally built for SharePoint. Support for FS4SP was added later, so there are some limitations to using KQL compared with the FAST-native FQL. However, for most search interfaces, KQL is a simple and effective query syntax.
KQL supports a number of search query operators, such as Boolean AND keyword queries that can consist of any term or terms, phrase queries, prefix matching (also known as wildcard search), and property queries. Wildcard search is supported only for prefix matching or “trailing wildcards,” where an asterisk (*) character may be placed at the end of several characters to match terms that start with the specified characters. KQL also supports Boolean operators and property operators such as equals (=) and contains (:). For more information about Boolean search logic, see Chapter 2.
Property operators can be applied to mapped properties. These operators take the form <property name><property operator><property value>. Table 8-1 outlines the available operators in KQL. You can apply a property query with or without a keyword query. However, if applied together without another operator, the property query will be added as a Boolean AND, thus returning a result set with items that contain all the keyword terms and only those for which the property query matches. Table 8-1 shows the keyword operators and Table 8-2 shows property query operators.
Keyword property operators (such as wildcard) are not available in property queries and vice versa.
Table 8-1. Keyword Query Syntax keyword operators
Table 8-2. Keyword Query Syntax property operators
Property Operators | Description | Supported Property types | Example |
---|---|---|---|
= | Equals | Text, DateTime, Integer, Decimal | Filetype=docx |
: | Contains | Text, Datetime, Integer, Decimal, Binary, YesNo | Author:Mikael |
< | Less than | DateTime, Integer, Decimal | >03/05/2007 |
> | Greater than | DateTime, Integer, Decimal | <06/24/2011 |
<= | Less than or equal | DateTime, Integer, Decimal | <=06/24/2011 |
>= | Greater than or equal | DateTime, Integer, Decimal | >=03/05/2007 |
<> | Does not equal | DateTime, Integer, Decimal | <>06/24/2011 |
.. | Range | DateTime, Integer, Decimal | 03/05/2007..06/24/2011 |
Fast Query Language (FQL) is a more powerful option for passing search queries to the FS4SP engine because it is the native query language of the FAST engine. (KQL queries are eventually translated to FQL when you run them.) FQL provides extended support for more complex queries than KQL, but mastering FQL takes more time, particularly to master more specialized queries. The XRANK operator is especially worth mentioning because you can use it to dynamically influence the ranking of search results. This provides a powerful and flexible way to change the ranking of your search results.
When using FQL as the query language, you cannot use the FS4SP keyword management features, such as synonyms, promotions/demotions, and best bets. When you use FQL in search scope definitions, this limitation does not apply.
FQL can be used only when integrating via the Query Object Model and the Query XML Web service. It is not exposed in the SharePoint search Web Parts.
FQL can be used with the Query Object Model and the Query Web Service to execute complex search queries. This may be desirable when KQL does not offer sufficient flexibility. A good example is when you request result sets that have custom ranking sequences. These sequences can be set by boosting term or property values in the query. These boost values are usually context-aware and hidden from the end user. This way, users do not realize their specific search would rank differently; they simply experience a more relevant result.
Although SharePoint search Web Parts do not support FQL, they can be modified to do so, and you can create new Web Parts that can use FQL. FQL is the native language for querying the FAST index and has several capabilities beyond KQL:
RANK and XRANK operators can be applied at query time to manipulate the ranking order of the result set based on term weighting or property weighting.
SORT and SORTFORMULA operators can be used to apply sorting to the result set.
The NEAR operator can be used to apply proximity search to the result set.
Complex queries based on a combination of the operators named in this list and a variety of other possible operators can be used (see Table 8-4).
Modified Web Parts that support FQL are available on CodePlex, courtesy of the SPSearchParts project team, at http://spsearchparts.codeplex.com.
FQL takes the query and splits it into operators and query strings. It isolates query strings rather than individual terms. These strings, whether text or some other value, are tokenized and special characters are removed. This is because not all characters are eligible to be tokenized and searched for.
FQL conforms to Augmented Backus-Naur Form (ABNF) syntax. Go to http://msdn.microsoft.com/en-us/library/ff394661.aspx for more details.
The identified operator takes a set of values enclosed in parentheses and can be limited by a scope specification. For example, the query title:string(“Swedish meatballs”, mode=“and”) passes a Boolean AND for Swedish meatballs in the title of the items. The specification title: limits the scope of the query to the title of the items in the search index; the string operator takes the values in parentheses and passes them with the limitation. In this case, Swedish AND meatballs is passed by the string operator. Simpler queries can be passed (such as title:“Swedish meatballs”), but you should be as specific as possible to avoid any reserved terms in the query language.
Although you don’t have to use quotation marks with the query strings in FQL, it is recommended that you do so to avoid FQL parsing errors with the reserved words. If you want to search for the reserved words any, and, or or, you should write your query like this: or(“any”, “and”, “rank”). A similar query without quotation marks and that is nonreserved would be or(apple, banana, orange), which is legal syntax. With quotation marks, it would be or(“apple”, “banana”, “orange”).
FQL can support nested sub-expressions and queries that consist of search tokens (keywords), properties, and operators. As in KQL, queries are limited to 2,048 characters, and queries are not case sensitive.
When you create complex FQL statements or include item URLs in your FQL statement, the 2,048-character barrier might be a limitation. Unfortunately, there is currently no way to change this, even if FAST can handle larger queries internally.
A scope specification limits a query to a particular field of indexed content. That field can be a managed property like body and title or a full-text index field that contains a set of managed properties. The scope specification prefixes the query specification with the In operator, which is set using a colon, for example, body:“Swedish meatballs”. If you do not define a scope specification, the default specification is used, which would be the default full-text index in the index profile definition. The default composite field or full-text index in FS4SP is named content.
FQL uses the asterisk character (*) or the question mark character (?) as wildcard characters for substitution expressions. Matches in any managed property or the full-text index may be matched by the wildcard character, where the asterisk matches zero or more character substitutions and the question mark matches only a single character. A wildcard expression matches only terms as they appear in the items before lemmatization and works at the token level on tokenized content. Therefore, wildcards will not match white space, characters treated as white space, or reserved characters. See Table 8-4 for examples. Also see Chapter 9, for an example of wildcard usage.
A number of words and characters are used in FQL itself, so those terms and characters are reserved. This does not mean you have to exclude the use of these terms in query strings, but you should take care to put these reserved words in double quotation marks to specify them as part of the query string so that they won’t function as operators. Although FQL accepts unquoted strings, you should always enclose search terms and phrases in double quotation marks anyhow because doing so avoids any possibility of conflict with reserved terms. The reserved terms are and, or, any, andnot, count, decimal, rank, near, onear, int, int32, int64, float, double, datetime, max, min, range, phrase, scope, filter, not, string, starts-with, ends-with, equals, and count.
Reserved characters are always removed, regardless of quotation encapsulation. Therefore, the queries title:(Swedish meatballs), title:(“[Swedish] <meatballs>”), and title:(“Swedish Meatballs”) are considered identical. Table 8-3 shows the reserved characters.
Table 8-3. Reserved characters used in non-quoted strings and for escaping
Reserved character | Usage |
---|---|
Newline | Escape character |
Carriage return | Escape character |
Tab | Escape character |
Backspace | Escape character |
Form feed | Escape character f |
Double quote | Escape character “ |
Single quote | Escape character ‘ |
Backslash | Escape character |
Tab | Can be used only within double quotation marks |
Newline | Can be used only within double quotation marks |
Carriage return | Can be used only within double quotation marks |
Space | Can be used only within double quotation marks |
( | Can be used only within double quotation marks |
) | Can be used only within double quotation marks |
, | Can be used only within double quotation marks |
/ | Can be used only within double quotation marks |
: | Can be used only within double quotation marks |
= | Can be used only within double quotation marks |
] | Can be used only within double quotation marks |
; | Can be used only within double quotation marks |
[ | Cannot be used as the first character outside of quoted text |
“ | Cannot be used as the first character outside of quoted text |
< | Cannot be used as the first character outside of quoted text |
> | Cannot be used as the first character outside of quoted text |
Many users will be familiar with common query syntax from global search engines on the World Wide Web. They may also be familiar with KQL and even with the use of the AND, OR, and NOT operators + and -. If Simple Query Language is enabled as part of an FQL query, you can pass simple query expressions by using + or -. The format uses the string operator with a mode parameter of simpleany or simpleall, which indicates a Boolean OR or Boolean AND search, respectively. Simple Query Language expressions that use quotation marks to specify a phrase must be escaped with a backslash (), for example, string(““Swedish meatballs” +recipe”, mode=“simpleall”).
A lot of attention is given to the XRANK operator and the ability to control ranking in the result set. FQL has RANK and XRANK operators, but XRANK is more powerful and performs better, so it is a natural choice. XRANK can also add dynamic rank to property queries, whereas RANK works only on the full-text index. XRANK takes two or more operands, which can be terms or sub-expressions in valid FQL. XRANK performs well in queries that return large result sets and can add dynamic rank, even to items that do not already have a rank by using the boostall=yes parameter. The ranking can be increased using the boost parameter to add ranking weight to specific terms or properties that may appear on items in the result set. The values added by XRANK are constant, whereas RANK adds value based on specific operands.
Although RANK is supported, using XRANK is much more flexible because you can control the boost value and use it against all managed properties and not just full-text indexes, which RANK is limited to.
The first operand of the XRANK operator is always the main query expression. This expression is matched in the index. All subsequent operands modify that result set by adjusting the rank values of its items. XRANK has an additional boost parameter that has a default value of 100. Using boost increases the rank of items that match a specific operand. The boost value can be set higher or lower. For example, the following expression boosts all items that contain meatballs by 200 if they also contain Swedish, as well as boosting those with the term Norwegian by 500.
xrank(xrank("meatballs","Swedish",boost=200),"Norwegian",boost=500, boostall=yes)
Adding boostall=yes to the outermost xrank of the preceding query ensures that items containing meatball, Swedish, and Norwegian are guaranteed to get a boost of 500—even if the items previously did not have any rank. With the default rank profile in FS4SP, all items will most likely have dynamic rank and setting the boostall parameter has no effect.
Table 8-4 lists the available operators in FQL and gives a brief explanation of their usage and syntax, and provides some examples. More information and examples are available from Microsoft at http://msdn.microsoft.com/en-us/library/ff394462.aspx.
Table 8-4. List of FQL operators and examples of usage
Operator | Description | Usage | Example |
---|---|---|---|
: | IN operator | For property queries, the property name precedes the IN operator. With other queries, the scope specification or operator precedes it, and parentheses containing the operands follow. | Title:“Recipe” Body:“Swedish meatballs” finds all items where Recipe is in the Title field and Swedish meatballs are in the Body field. |
[a] or ? | Wildcard | Use[a] for matching zero or more characters in a word or phrase. Use ? For matching a single character. White space is not matched with a wildcard. The number of matching characters for a[a] wildcard can be set using the string operator operands minexpansion and maxexpansion—that is, string(S, minexpansion=<n1>, maxexpansion=<n2>), where S is the string with wildcards and n can be any value between zero or more. WarningEven though starting a search term with a wildcard is syntactically supported, it expands to more than 10,000 terms and generates a “1012 – resource limit exceeded” error in most cases. Wildcards can, however, be used directly against a managed property if the property contains fewer than 10,000 unique terms. | Adding a wildcard to match zero or more trailing characters: meatba[a] Adding a wildcard to match zero or more preceding characters: [a]ish Adding a wildcard to match zero or more characters within a term or phrase: m[a]balls Adding a wildcard to match a single character: mea?balls Using a wildcard with the string operator: string:(“Swedi?h me[a]lls”) |
string | STRING | The string parameter can be used to easily pass a text string from the query box in FQL. The string operator takes the following syntax. string(“<text string>” [, mode=<mode>] [, n=<near>] [, weight=<n>] [, linguistics=<on|off>] [, fuzzy=<on|off>] [, fuzzythreshold=<threshold>] [, fuzzycutoff=<cutoff>] [, fuzzycutoff=<cutoff>] [, wildcard=<off|basic|ext>]) where mode=“PHRASE”|“AND”|“OR”|“ANY”|“NEAR”|“ONEAR”|“SIMPLEANY”|“SIMPLEALL” | string(“Swedish meatballs”, mode=“AND”) |
and | AND operator | The AND operator matches items that contain all operands. Operands can be terms or nested valid FQL sub-expressions. The syntax is as follows. and(operand, operand [, operand][a]) | and(Swedish, meatballs) returns only items with both the terms Swedish and meatballs in them. |
or | OR operator | The OR operator matches items that satisfy any of the operands. Operands can be terms or nested valid FQL sub-expressions. | or(Swedish, meatballs) returns all items with either the term Swedish or meatballs in them. |
not | Negation operator | The negation operator returns only items that do not match the operand. not(operand) | not(Norwegian) returns items that satisfy any other part of a larger query and do not contain Norwegian. |
any | OR operator | Similar to the OR operator except that the dynamic rank (the relevance score in the result set) is affected by neither the number of operands that match nor the distance between the terms in the item. The operands may be a single term or any valid FQL sub-expression. | or(Swedish, meatballs) returns all items with either Swedish or meatballs in them. Items with both words are not ranked higher than those with just one of the words. |
andnot | ANDNOT operator | The andnot operator matches all items that contain the first operand and do not match all subsequent operands. So, if all operands are terms, the result set will contain items that contain the first term but do not contain all remaining terms. Operands can, however, be nested expressions and sub-expressions as long as they are valid FQL. andnot(operand, operand [,operand][a]) | andnot(meatballs, Danish, Norwegian) returns all items with the term meatballs in them but not items with Danish or Norwegian. |
equals | Property equality operator | The equals operator applies to properties for matching exact terms or phrases to content in a specific property. equals(<term or phrase>) | author:equals(“Marcus Johansson”) returns all items with the exact phrase Marcus Johansson in the author property. |
near | NEAR operator | Specifies that terms in the query must appear within a specific distance or tokens where the operator takes two or more terms and a numeric value to specify the distance N. The default numeric value is 4. near(arg, arg [, arg][a] [, N=<numeric value>]) | near(Swedish, meatballs, recipe, N=10) returns items that have the terms within 10 tokens of each term. |
onear | Ordered NEAR | Ordered variant of the NEAR operator, where the terms must appear in the specified order. | onear(Swedish, meatballs, recipe) returns items with the terms Swedish, meatballs, and recipe, and only in that order. |
count | COUNT operator | The count operator specifies a range of hits that must appear on an item for it to be returned. A minimum or maximum alone may be defined. count(term [,from=<numeric value>, to=<numeric value>]) | count(meatballs, from=10, to=50) returns items that had at least 10 but not more than 50 occurrences of the term meatballs. |
rank | RANK modifying operator | RANK boosts the dynamic rank of matching items in a result set without changing the composition of the result set. The first operand is used for creating the result set and the following operands are used for modifying the dynamic rank. rank(operand, rank-operand [, rank-operand][a]) | and(recipe, rank(meatballs, Swedish)) returns all items that contain both recipe and meatballs but ranks higher those that contain Swedish. |
xrank | XRANK rank modifying operator | XRANK allows for modification of the ranking values in the result set based on an expression and a boost value. XRANK can be applied against both the full-text index and managed properties. The queries take the following format. xrank(operand, rank-operand [, rank-operand][a] [,boost=n].[,boostall=yes]) | xrank(and(Swedish, meatballs), spicy, boost=200, boostall=yes) returns all items with Swedish and meatballs and boosts any item by 200 with the term spicy in it. |
range | RANGE operator | This operator enables range matching expressions on numeric and date/time managed properties. The format of the operator is as follows. range(start, end [,from=“GE”|“GT”] [,to=“LE”|“LT”]) Default value for from is GE and default value for to is LT. GE = Greater than or Equal GT = Greater than LE = Less than or Equal LT = Less than You can use the values of min and max in the from and to parameters to specify an absolute boundary in that range direction. | write:range(min, 2012-03-01T00:00:00) matches items older than and not including 2012-03-01T00:00:00 write:range(2012-01-01T00:00:00, max, from=GT) matches items modified after and not including 2012-01-01T00:00:00 write:range(2012-01-01T00:00:00, 2012-03-01T00:00:00, from=GT, to=LT) matches items newer than 2012-01-01T00:00:00 and up to but not including 2012-03-01T00:00:00 and(size:range(15000, max), description:starts-with(“fs4sp”)) matches items where the descriptions starts with fs4sp and the item size is at least 15,000 bytes. |
phrase | PHRASE | Returns only items that have the query string in a phrase. phrase(term [, term][a]) | phrase(Swedish, meatballs) returns items with the phrase Swedish meatballs in them. |
filter | FILTER | The filter operator is used for filtering result sets based on metadata property matches. The filter operator cannot be used with linguistics and it will be set to off. This operator is often nested in a broader query expression to delimit that expression based on available property matches. filter(<any valid FQL operator expression>) Refiners in FS4SP use the filter operator internally when matching values. | and(body:“Swedish meatballs”, title:recipes, filter(doctype:equals(“pdf”))) returns items with Swedish meatballs in the body text, recipes in the title, and of item type pdf. |
starts-with | STARTS-WITH property operator | Specifies that items in the result set must start with the specified term or phrase in the specified property. starts-with(<term or phrase>) Only applies to properties. | The query author:starts-with(“Marcus”) matches all items that have an author property that starts with Marcus. |
ends-with | END-WITH property operator | Specifies that items in the result set must end with the specified term or phrase in the specified property. ends-with(<term or phrase>) | The query author:ends-with(“Johansson”) matches all items that have an author property that ends with Johansson. |
int | INTEGER | 32-bit signed integer for matching numeric term expressions. Mainly used for performance improvements because the FQL query parser is skipped and the values are sent directly to the query matching component. | and(string(“fs4sp”), filter(id:int(“1 20 49 124 453 985 3473”, mode=“or”))) matches items with the word fs4sp and one of the numbers specified for the id managed property. |
float | FLOAT | Float or double for matching numeric term expressions. Usually not needed because the type of the query term is detected according to the type of the target numeric managed property. | float(42) treats the number as a floating point |
formula | Sort Formula | A formula set for sorting the result set formula:<sort-formula>. You can use multiple methods for sorting.[a] | formula:abs(20-height) |
datetime | DATETIME | Operator for matching Date-time expressions. Supported formats are as follows: YYYY-MM-DD, YYYY-MM-DDThh:mm:ss, YYYY-MM-DDThh:mm:ssZ where YYYY is a four-digit year, MM is a two-digit month, and DD is a two-digit day. The letter T specifies an expected time, hh is a two-digit hour in the 24-hour clock, mm is a two-digit minute, and ss is a two-digit second. The letter Z stands for Zulu time or Coordinated Universal Time (UTC). | datetime(“2011-12-25T12:00:00Z:) returns items tagged with the datetime property for exactly noon on December 25th, 2011 UTC. |
[a] For more information about ranking and sorting, go to http://msdn.microsoft.com/en-us/library/ff394654.aspx. |
When you execute searches in a Search Center in SharePoint, you will notice that the query string changes and that your query parameters get turned into URL parameters. For example, if you search for the term meatball, the URL will look like the following after the query is executed.
http://intranet.contoso.com/search/Pages/results.aspx?k=meatball
The search term is added after the k query string parameter in the URL. The URL syntax can be used both against the Result.aspx page of your Search Center and against the srchrss.aspx page for retrieving the results as an RSS feed. Table 8-5 lists the available parameters to use when constructing a URL, and Table 8-6 lists RSS feed–only parameters.
Table 8-5. URL query string parameters for Search Center and the RSS search page use
URL parameter | Description | Example |
---|---|---|
k | Specifies the keywords. | results.aspx?k=swedish%20meatballs Note that special characters such as the space are URL-encoded. |
v | Specifies the result view that is to be used for the query. This parameter must be either:
| results.aspx?v=write (sorts on last modified date) |
start | Specifies which results page number to show. | results.aspx?start=11 (skips the first 10 results) |
s | Specifies the search scope. This parameter can contain multiple values, separated by %2c, as shown in the second example to the right. | results.aspx?s=SDKContent results.aspx?s=SDKContent%2cTechnicalArticles |
a | Appends text to your query but is not shown in the search box. | results.aspx?k=fs4sp&a=author%3Amikael This adds “author:mikael” to the query and is the equivalent of sharepoint author:Mikael. |
u | Limits items to a certain URL or site. | results.aspx?k=fs4sp&u=http://contoso.com This is equivalent to fs4sp site:“http://contoso.com”. |
r | Specifies refiners in an encoded format. | See Chapter 9 for an example of how to build the r= parameter. |
similarto simlartype | Used when enabling “Find similar” functionality. The value of similarto is retrieved from the docvector managed property. The value of similartype is as follows:
| results.aspx?k=red&similarto=[adventure works, 1][aspx, 0.707107]&similartype=find |
Table 8-6. RSS feed–specific URL parameters
URL parameter | Description | Example |
---|---|---|
provider | Used to specify the search provider when running against srchrss.aspx (RSS feed) | Required and must be set to FASTSearch. srchrss.aspx?k=fs4sp&provider=FASTSearch |
count | Used to specify the page size | srchrss.aspx?k=fs4sp&provider=FASTSearch&count=5 Returns five items in the search result. |
lang | Used to set the query language | srchrss.aspx?k=fs4sp&provider=FASTSearch&lang=en Will execute the query with English as the query language. |
es | Used to specify whether lemmatization is on or off for the query | By default, lemmatization is turned off. es=1 enables lemmatization |
If your search results page contains multiple queries, you can specify which query the parameter applies to by appending a number from 1 through 5, indicating the Search Query Results ID between the parameter and the “=” character in the URL. However, this is not possible for the k parameter, so you have to use the same search terms for all queries on the page.
Three groups of APIs are available for executing search queries against FS4SP:
Federated Search Object Model
Query Object Model
Query Web Service
The first two are used when developing applications that are hosted inside of SharePoint, and the last one is primarily used when an external application wants to query against FS4SP. For example, this can be a web portal created in PHP or a plug-in in Microsoft Outlook.
In addition to the Query Web Service, you can use a second option when querying FS4SP from outside of SharePoint. The Search Center in SharePoint allows results to be returned as an RSS feed. Crafting search queries that you run against the Search Center URL to return RSS is an easy way to get results back.
RSS is a standardized format of XML that is used to publish frequently updated works such as blog entries, news headlines, audio, and video. An RSS document includes full or summarized text in addition to metadata such as publishing dates and authorship. Go to http://en.wikipedia.org/wiki/Rss for more information about RSS.
An overview of how the different query options tie into each other is shown in Figure 8-3.
As you see from the diagram in Figure 8-3, all the query APIs end up using the KeywordQuery class for the actual query execution. This chapter examines each of the query APIs that are available in SharePoint and shows example usage.
To return all available search results for a user with FS4SP, you can use the search term #. You can use the hash sign to return all results for all available query APIs.
The API classes available for querying the search index are listed in Table 8-7.
Table 8-7. Query API classes available when developing SharePoint applications with FS4SP
URL parameter | Namespace | Example |
---|---|---|
SharedQueryManager | Microsoft.Office.Server.Search.WebControls | Microsoft.Office.Server.Search in Microsoft.Office.Server.Search.dll |
RefinementManager | Microsoft.Office.Server.Search.WebControls | Microsoft.Office.Server.Search in Microsoft.Office.Server.Search.dll |
QueryManager | Microsoft.Office.Server.Search.Query | Microsoft.Office.Server.Search in Microsoft.Office.Server.Search.dll |
Location | Microsoft.Office.Server.Search.Query | Microsoft.Office.Server.Search in Microsoft.Office.Server.Search.dll |
LocationList | Microsoft.Office.Server.Search.Query | Microsoft.Office.Server.Search in Microsoft.Office.Server.Search.dll |
FASTSearchRuntime | Microsoft.Office.Server.Search.Query | Microsoft.Office.Server.Search in Microsoft.Office.Server.Search.dll |
KeywordQuery | Microsoft.Office.Server.Search.Query | Microsoft.Office.Server.Search in Microsoft.Office.Server.Search.dll |
In addition to using the search APIs via SharePoint, you have the option to query a QR Server directly. This works only when logged onto an FS4SP server that has the QR role because you have to access it with localhost.
You can read more about the QR Server in Chapter 3.
If you installed FS4SP and used 13,000 as the base port, you can access the QR Server at http://localhost:13280 (http://localhost:13280/) via a web browser, as shown in Figure 8-4.
When you access the QR Server webpage, the page opens by default on the search page tab, and you can execute queries in FQL. The page can be used for testing and debugging purposes in order to see if you get any results back.
Keep in mind that the queries you execute are not passing user credentials; therefore, you get back only results that are publicly available unless you modify <FASTSearchFolder>etcqrserverqtf-config.xml and comment out the line.
<instance-ref name="securityfql" critical="1" />
This effectively disables all security trimming for your search results, including those executed via SharePoint. Remember to restart the QR Server process in order for the change to be effective.
Most of the options seen in Figure 8-4 are equal to those used in the available SharePoint query APIs, but some of the features listed are not available via the normal query APIs. This is because the page has been carried over from FAST ESP without being modified to fit the available query APIs in SharePoint.
One example is GeoSearch, which is a prebuilt version of geographical search where you search for items within a radius from a latitude and longitude coordinate. This is typically used to find, for example, hotels close to a specific address. The built-in geo search feature is not available via the query APIs; instead, you have to apply a sort formula in which you calculate the radius yourself.
The QR Server contains several pages with useful information. The Configuration page lists a lot of statistics on search queries, what steps a search query goes through in the query pipeline, and information about the different managed properties set up. The Reference page has a short guide about how to write FQL queries.
With the Federated Search Object Model, you can bring results together from multiple search engines or repositories at the same time. The Federated Search Object Model allows you to query against SharePoint Server search, FS4SP, OpenSearch locations, and custom-developed Location run times.
An example usage scenario for a custom run time is to implement a run time that executes search queries against a Microsoft SQL Server database. This allows you to use the search Web Parts in SharePoint to query against your database without the need to create a custom search page.
The search result Web Parts in SharePoint are built using the Federated Search Object Model. An example of this is the default result page that shows results from FS4SP in the middle and people results in the right column. Results from two different search engines are returned via the same object model, in this case, the page’s SharedQueryManager.
The search Web Parts included in SharePoint use the SharedQueryManager class to get a reference to the current page’s QueryManager. All search Web Parts displaying information from the same result set share this QueryManager.
When you use QueryManager to search, results are returned as XML. The returned XML is formatted to work with the XSLT transformation included in the search Web Parts that turn the XML into the HTML displayed to the end user.
An example of two Web Parts sharing a QueryManager is the core results Web Part and the refinement Web Part. During search execution, they each register which parts of the search result they want returned and only one query is sent over to FS4SP. This returns both refiner results and the search hits at the same time. The refiners are retrieved via the RefinementManager class, which is the refiner equivalent to the SharedQueryManager. RefinementManager in turn uses the QueryManager internally.
The primary use for the QueryManager and RefinementManager is when you create a Web Part that inherits from CoreResultsWebPart or RefinementWebPart, or when you create a custom Web Part that is to be placed on a page together with one of the aforementioned Web Parts.
The QueryManager is implemented as a List<LocationList>, as shown in Figure 8-3, and has very few options that you can set. These options are described in Table 8-8.
Table 8-8. Properties of the QueryManager class
Property | Description |
---|---|
DefaultFASTSearchSort | Gets or sets the sort property used with FS4SP. This can be a sortable managed property, the name of a rank profile, or the name of a full-text index. Prepend the name with + for descending sort and - for ascending sort. Default value: +default(default is the name of the default rank profile) |
Timeout | Gets or sets the query timeout value for the federated search location. Default value: 90,000 milliseconds (ms) |
TrimDuplicates | Gets or sets a Boolean value that specifies whether duplicate items should be removed from the search results. Default value: true |
UserQuery | Gets or sets the search query to send to the federated location. |
Find the QueryManager class in the Microsoft.Office.Server.Search.Query namespace. MSDN documentation about this class can be found at http://msdn.microsoft.com/en-us/library/microsoft.office.server.search.query.querymanager.aspx.
The LocationList class contains a list of federated locations and is implemented as a List<Location>. As you move down the class hierarchy, you can see that the class options get more specific toward the search being executed, as shown in Table 8-9.
Table 8-9. Properties of the LocationList class
Description | |
---|---|
ItemsPerPage | Gets or sets the number of search results to display per page. Default value: 10 (Core Results Web Part) |
ReturnedResults | Gets the number of search results returned for the query. The count is an aggregate of the number of returned results per Location. |
StartItem | Gets or sets the number of the first item returned in the search results. Default value: 1 |
TotalResults | Gets the total number of search results returned for the query. The count is an aggregate of the total number of returned results per Location, excluding duplicate entries if duplicate removal has been enabled for the query. |
TotalResultsIncludingDuplicates | Gets the total number of search results returned for the query, including duplicate results. The count is an aggregate of the total number of returned results per Location, including duplicate entries. |
The LocationList class can be found in the Microsoft.Office.Server.Search.Query namespace. MSDN documentation about this class can be found at http://msdn.microsoft.com/en-us/library/microsoft.office.server.search.query.locationlist.aspx.
The Location class represents a federated location. A federated location is any search location listed under Federated Locations on your FAST Query SSA in SharePoint Central Administration. For FS4SP, the Location is named Local FAST Search Results, or FASTSearch, which is the internal name you have to use when referring to it, as shown in Table 8-10.
Table 8-10. The display names and internal names of preinstalled search locations in SharePoint
Display name | Internal name |
---|---|
Internet Search Results | InternetSearchResults |
Internet Search Suggestions | InternetSearchSuggestions |
Local Search Results | LocalSearchIndex |
Local People Search Results | LocalPeopleSearchIndex |
Local FAST Search Results | FASTSearch |
If you want to query additional Internet sources from within SharePoint, you can download several federated search connectors from TechNet at the Federated Search Connector Gallery for Enterprise Search. Go to http://technet.microsoft.com/en-us/enterprisesearch/ff727944.aspx. The files contain OpenSearch 1.1 definition files and include XSLT, both of which work with the search Web Parts in SharePoint.
The Location class has 21 properties that work with FS4SP; these classes are listed in Table 8-11. The other properties available are SharePoint Search specific.
Table 8-11. Properties of the Location class available for use with FS4SP
Property | Description |
---|---|
CurrentUserProfile | Gets or sets the user profile to use for the search query. Used when you enable contextual search scopes as described in the section User Context Management in Chapter 6. |
DefaultOperator | Gets or sets the default operator. Used to specify whether the keywords used in the query should have AND or OR between them as default. Default value: AllKeywords (AND) |
EnableStemming | Gets or sets a Boolean value specifying whether stemming is enabled. For FS4SP, this property enables lemmatization and not stemming. See Chapter 2 for an explanation of the difference between stemming and lemmatization. |
Exception | Gets the exception returned from the federated location. |
HitHighLightProperties | Gets the list of hit-highlighted properties for the current result set. |
ItemsPerPage | Gets or sets the number of search results to display per page. |
QueryInfo | Gets the query log data for a single click-through for the search query. |
QueryModification | Gets or sets the modified query. The QueryModification property is used with the Did You Mean search feature. |
RefinementFilters | Gets or sets the set of refinement filters used when issuing a refinement query. |
RequestedLanguage | Gets or sets the language for the query. |
RequestedProperties | Gets or sets the list of properties that should be included for each item in the search results. |
Result | Gets the search result set for the federated location. Make sure the query has been executed before accessing this property. If you don’t, the property returns null. |
ReturnedResults | Gets the number of search results returned for the query. |
SpellingSuggestion | Gets or sets an alternate spelling for a search term that is not recognized. |
StartItem | Gets or sets the number of the first item returned in the search results. |
SupplementaryQueries | Gets or sets additional terms to append to the search query. |
TimeZone | Gets or sets the time zone for the search query. |
TotalResults | Gets the total number of search results returned from the federated location for the query. |
TotalResultsIncludingDuplicates | Gets the total number of search results returned for the query, including duplicate results. |
TrimDuplicates | Gets or sets a Boolean value that specifies whether duplicate items should be removed from the search results. |
UserQuery | Gets or sets the query to send to the federated location. |
The Location class can be found in the Microsoft.Office.Server.Search.Query namespace. MSDN documentation for this class can be found at http://msdn.microsoft.com/en-us/library/microsoft.office.server.search.query.location.aspx.
The FASTSearchRuntime is what ties together the Location definition with the KeywordQuery class and runs the query via the FAST Query SSA over to the FS4SP farm, as shown in Figure 8-3. Together with the KeywordQuery class, the FASTSearchRuntime class is the only class that you can use to enable FQR for a search query and specify how you want the results sorted. In addition, several properties control how the result should be performed and formatted, and there are properties for controlling the refiners. All available properties are listed in Table 8-12.
Table 8-12. Properties of the FASTSearchRuntime class
Property | Description |
---|---|
Config | Sets the configuration information for the FS4SP farm location. The configuration for FASTSearchRuntime is viewable when looking at Local FAST Search Results below Federated Locations on your FAST Query SSA in SharePoint Central Administration. |
EnableFQL | Gets or sets a Boolean value that specifies whether the query string is written in FQL syntax. If set to false, the query string is interpreted using the Keyword Query syntax. |
EnableSpellcheck | Gets or sets the flag for how spelling corrections and suggestions are handled for a query. Values are:
|
Location | Gets or sets the FS4SP location. |
MaxShallowRefinementHits | Gets or sets the number of results to use to calculate shallow refinement results. |
MaxSummaryLength | Gets or sets the maximum character length of the hit highlighted summary. This value is not a hard limit but a hint to the dynamic summary generator to try to limit the summary length to this value. Default value: 185 Maximum value: ~400 with current FS4SP internal settings |
MaxUrlLength | Gets or sets the maximum character length of the hit highlighted url (hhurl) element. The hit highlighted url element is truncated if it is longer than this value. Default value: 2048 |
RefinementManager | Gets or sets an object representing the refinement manager that configures the query refinement filters. |
Refiners | Gets or sets a comma-separated list of refiners to return in the search result. No refiners are returned with the query result if the Refiners property is empty. |
ResubmitFlags | Gets or sets the flag specifying the criteria for automatically resubmitting a query that yielded no results. |
SimilarTo | Contains a string that represents the similarity reference when searching for similar items. |
SimilarType | Specifies the type of Find Similar query to be performed. Values are:
|
SortList | Gets or sets the list of properties by which the search results are ordered. |
SortSimilar | A Boolean value that specifies that query results based on similarity shall be sorted by similarity. If set to false, the result is sorted by relevance. |
SupportsAsync | Gets a Boolean value that specifies whether the FS4SP run time supports asynchronous query requests. Default value: Always returns false. |
TrimDuplicatesIncludeId | Specifies the value associated with a collapse group, typically used when a user clicks the Duplicates (n) link of an item with duplicates. This value corresponds to the value of the fcoid managed property that is returned in query results. Go to http://msdn.microsoft.com/en-us/library/ff521593.aspx for more information about duplicate removal and hit collapsing with FS4SP. |
TrimDuplicatesKeepCount | An attribute that specifies how many items to keep for each set of duplicates. Default value: 1 |
TrimDuplicatesOnProperty | Indicates the name of a nondefault managed property to use as the basis for duplicate removal. |
UserContextGroupID | Gets or sets the identifier for getting the correct keyword settings, such as synonyms, best bets, and visual best bets. The ID is the Site Collection ID for the site collection where these keyword features have been configured. |
VisualBestBets | Gets a list of VisualBestBetHelper objects that hold visual vest bet information for the query result. |
XmlHighConfDoc | Gets an XML representation of the best bets. |
Find the FASTSearchRuntime class in the Microsoft.Office.Server.Search.Query namespace. MSDN documentation for this class can be found at http://msdn.microsoft.com/en-us/library/microsoft.office.server.search.query.fastsearchruntime.aspx.
In addition to the FASTSearchRuntime, the Federated Search Object Model includes two more run times: the SharePointSearchRuntime and the OpenSearchRuntime. With FS4SP, the SharePointSearchRuntime is used for People Search, and the OpenSearchRuntime is used for any external search location, such as Bing News. You can also create your own search Location classes using a custom search run time.
When using the QueryManager class to execute a search query, you have to use both LocationList and Location. The following sample illustrates the use of the SharedQueryManager to execute two different queries against FS4SP inside of a Web Part on a SharePoint page.
// Get a reference to the Query SSA Proxy var searchProxy = (SearchServiceApplicationProxy) SearchServiceApplicationProxy. GetProxy(SPServiceContext.Current); // Get a reference to the SharePoint page's query manager var qm = SharedQueryManager.GetInstance(Page).QueryManager; // Create a new LocationList object var locationList = new LocationList(); // Create a Location object for query #1 and add to the list var fastLocationOne = new Location("FASTSearch", searchProxy); fastLocationOne.UserQuery = "FAST Search for SharePoint"; fastLocationOne.ItemsPerPage = 2; locationList.Add(fastLocationOne); // Create a Location object for query #2 and add to the list var fastLocationTwo = new Location("FASTSearch", searchProxy); fastLocationTwo.UserQuery = "Microsoft"; fastLocationTwo.ItemsPerPage = 2; locationList.Add(fastLocationTwo); // Add and trigger the federated locations qm.Add(locationList); qm.IsTriggered(locationList); // Execute the query and get merged XML XmlDocument resultXml = qm.GetResults(); // Number of results returned - 4 in total (2+2) int returnedResults = locationList.ReturnedResults; // Total number of results aggregated for both queries int totalResults = locationList.TotalResults;
You can see another detailed example of querying with the Federated Object Model at http://msdn.microsoft.com/en-us/library/ff407955.aspx.
The following example shows the XML output returned from the previous code. The sample has been shortened for readability. The interesting part is that the XML for each location is merely appended after each other. First are two results for the query FAST Search for SharePoint with id 1 and 2, followed by the number of results and total results for that query. Next are the two results from the query Microsoft, also with id 1 and 2, and the number of results and total results. Appending result sets in this manner makes parsing the XML more complicated. The use of GetResults() from the QueryManager might not be the best way to get the result XML when you query more than one federated location at a time.
<All_Results> <Result> <id>1</id> <rank>18890</rank> <title>FS4SP Book Project</title> <size>3793511</size> <path>http://test/Shared Documents/FS4SP book Project.pptx</path> <write>5/16/2011</write> <contentclass>STS_ListItem_DocumentLibrary</contentclass> <isdocument>True</isdocument> <url>http://test/Shared Documents/FS4SP book Project.pptx</url> <fileextension>PPTX</fileextension> </Result> <Result> <id>2</id> <rank>18862</rank> <title>FS4SP Workshops</title> <size>2092916</size> <path>http://test/Shared Documents/FS4SP presentation.pptx</path> <write>5/27/2011</write> <contentclass>STS_ListItem_DocumentLibrary</contentclass> <isdocument>True</isdocument> <url>http://test/Shared Documents/FS4SP presentation.pptx</url> <fileextension>PPTX</fileextension> </Result> <TotalResults>3</TotalResults> <NumberOfResults>2</NumberOfResults> <Result> <id>1</id> <rank>1921</rank> <title>Microsoft Partner Program</title> <size>281600</size> <path>http://test/Shared Documents/Platform-Test-Framework-1.0.doc</path> <write>11/30/2004</write> <contentclass>STS_ListItem_DocumentLibrary</contentclass> <isdocument>True</isdocument> <url>http://test/Shared Documents/Platform-Test-Framework-1.0.doc</url> <fileextension>DOC</fileextension> </Result> <Result> <id>2</id> <rank>1582</rank> <title>Processing components.docx</title> <size>239493</size> <path>http://test/Shared Documents/Processing components.docx</path> <write>6/23/2011</write> <contentclass>STS_ListItem_DocumentLibrary</contentclass> <isdocument>True</isdocument> <url>http://test/Shared Documents/Processing components.docx</url> <fileextension>DOCX</fileextension> </Result> <TotalResults>18</TotalResults> <NumberOfResults>2</NumberOfResults> </All_Results>
In order to avoid the appending of XML, you can remove the following line.
XmlDocument resultXml = qm.GetResults(locationList);
Replace it with the following two lines.
XmlDocument locationOneXml = fastLocationOne.GetResults(qm); XmlDocument locationTwoXml = fastLocationTwo.GetResults(qm);
This way, you get one XML document per result set, which is easier to work with.
If you query two different locations, for example, FASTSearch and InternetSearchResults, FASTSeach will return XML as just shown, but InternetSearchResults will return XML in RSS format. Merging two completely different XML formats is not a good idea and emphasizes that using GetResults() on the Location object is a better approach.
The previous code sample has duplicate trimming turned on by default, but both TotalResults and TotalResultsIncludingDuplicates return the same total even if there are duplicates being removed from the result set.
In order to get the exact number of results when TrimDuplicates is enabled, you have to enable refiners for your search query. Because duplicate trimming is applied only to search results and not to refiner results, the total count of a refiner can be higher than that of the search result if there are duplicates within the result set.
Internally, the TotalResultsIncludingDuplicates property is assigned from the refiner values if there is a refiner with a higher value than that of the TotalResults property. The refiner property has to cover all returned items in order to get the correct count. Examples of managed properties that you can use are write and size, which both contain values for all items. When using the default configuration of the RefinementManager, write is included.
If you modify the code sample shown in the “Executing a Query by Using QueryManager, LocationList, and Location” section earlier in this chapter with refiner settings added, you get the correct count in TotalResultsIncludingDuplicates. Look at the code that follows.
RefinementManager requires a Location object on initialization. If you add more than one Location that supports refiners to your query, you have to choose which one you will run a refiner on. To get around this limitation, you can instead execute a search per location and manually calculate the total count with and without duplicates.
var searchProxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy. GetProxy(SPServiceContext.Current); var qm = SharedQueryManager.GetInstance(Page).QueryManager; var locationList = new LocationList(); var fastLocationOne = new Location("FASTSearch", searchProxy); fastLocationOne.UserQuery = "FAST Search for SharePoint"; fastLocationOne.ItemsPerPage = 2; locationList.Add(fastLocationOne); qm.Add(locationList); qm.IsTriggered(locationList); // Get a reference to the SharePoint page's refinement manager RefinementManager refinementManager = RefinementManager.GetInstance(this.Page); // Use the default refiner configuration for FAST refinementManager.UseDefaultConfiguration = true; // Initialize the use of refiners on the query refinementManager.Initialize(fastLocationOne); XmlDocument resultXml = qm.GetResults(); int returnedResults = locationList.ReturnedResults; int totalResults = locationList.TotalResults; int totalResultsIncludingDuplicates = locationList.TotalResultsIncludingDuplicates;
If you want to return the refinement XML from RefinementManager, you must have a CoreResultsWebPart on the same page. If you don’t, the following code returns null.
RefinementManager refinementManager = RefinementManager.GetInstance(this.Page); XmlDocument refinementXml = refinementManager.GetRefinementXml();
Querying with FASTSearchRuntime requires an instance of a Location object that points to FASTSearch and a reference to the Query SSA Proxy. The following code sample executes a search for microsoft by using FQL and sorts the results with the newest items on top using FASTSearchRuntime.
// Get a reference to the Query SSA Proxy var searchProxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy. GetProxy(SPServiceContext.Current); FASTSearchRuntime runtime = new FASTSearchRuntime(searchProxy); // Create a Location object for FASTSearch runtime.Location = new Location("FASTSearch", searchProxy); // Enable queries written in FQL runtime.EnableFQL = true; // Set sort order runtime.SortList.Add("write", SortDirection.Descending); // Execute query XmlDocument resultXml = runtime.SendRequest("string("microsoft")");
The Query Object Model in SharePoint consists of the FullTextSqlQuery class and the KeywordQuery class. The FullTextSqlQuery class is not available for use with FS4SP; however, in addition to Keyword Query Syntax, the KeywordQuery class supports the use of the FQL, which removes the need for an additional query class.
Although the Federated Search Object Model allows you to query any search engine defined under Federated Locations on your FAST Query SSA in SharePoint Central Administration, the KeywordQuery class can be used only to query SharePoint Search or FS4SP.
When developing custom search applications that are not based around the search Web Parts included in SharePoint, the KeywordQuery class offers the most flexibility compared to the APIs in the Federated Search Object Model in terms of the parameters available when specifying a search query, as listed in Table 8-13. When using KeywordQuery, the search results are returned as a ResultTableCollection and not as XML like the Federated Search Object Model APIs.
Table 8-13. Properties of the KeywordQuery class
Property | Description |
---|---|
AuthenticationType | Gets or sets the authentication type for the query. Values are:
|
Culture | Gets or sets the locale for the query. |
CustomRefinementIntervals | Gets or sets the refinement intervals that should be imposed on the refinements. |
DirectServiceEndpointUri | Gets or sets the address that the Search service application proxy should use instead of the URL specified in the constructor for the Search service application proxy. |
EnableFQL | Gets or sets a Boolean value that specifies whether the query string is according to the FQL syntax. |
EnablePhonetic | Gets or sets a Boolean value that specifies whether the phonetic forms of the query terms are used to find matches. Only applicable for People Search. |
EnableSpellcheck | Gets or sets the flag for how spelling corrections and suggestions are handled for a query. Values are:
|
EnableStemming | Gets or sets a Boolean value that specifies whether stemming is enabled. For FS4SP, this property enables lemmatization and not stemming. See Chapter 2 for an explanation of the difference between stemming and lemmatization. |
HitHighlightedProperties | Gets the collection of hit-highlighted properties for the query. |
KeywordInclusion | Gets or sets a value that specifies whether the query returns results that contain all or any of the specified search terms. This property is only applicable to queries using the Keyword Query Syntax. |
MaxShallowRefinementHits | Gets or sets the number of results to be used to calculate shallow refinement results. In most cases, you use deep refinement with FS4SP and can ignore this property. |
MaxSummaryLength | Gets or sets the maximum length of the search result summary. This value is not a hard limit but a hint to the dynamic summary generator to try to limit the summary length to this value. |
MaxUrlLength | Default value: 185 |
QueryInfo | Maximum value: ~400 with current FS4SP internal settings |
QueryText | Gets or sets the text for the search query. |
RefinementFilters | Gets or sets the set of refinement filters used when issuing a refinement query. A string containing a set of refinement tokens separated by space. Refinement tokens are returned as part of the RefinementResults table for the previous query. |
Refiners | Gets or sets the set of refiners to be returned in a search result. The value for each refiner is a comma-separated format string that specifies the name of the managed property to use as a refiner and, optionally, a set of refinement options for this refiner. No refiners are returned with the query result if the property is empty, which it is by default. When specifying refiners, you can add parameters such as sorting and return intervals. See http://msdn.microsoft.com/en-us/library/gg984547.aspx for reference on how you can customize refiner configuration. |
ResubmitFlags | Gets or sets the criteria for automatically resubmitting a search query that yielded no results. Values are:
See http://msdn.microsoft.com/en-us/library/microsoft.office.server.search.query.resubmitflag.aspx for reference on these options. |
ResultsProvider | Gets or sets the search provider used for the search query. Values are:
|
ResultTypes | Gets or sets a value that specifies the search result type. Values are:
|
RowLimit | Gets or sets the maximum number of items returned in the search results. This is the page size you want returned. Used in conjunction with StartRow. Maximum value is 10,000. |
RowsPerPage | Not used with FS4SP. |
SearchTerms | Gets the terms used for the search query. |
SelectProperties | Gets a list of the properties to return in the search results. |
SimilarTo | Gets or sets the similarity reference when performing a Find Similar query. |
SimilarType | Gets or sets the type of Find Similar query to perform. Values are:
|
Site | Gets the site the search query is executed from. |
SiteContext | Gets or sets the site URL for the search query. |
SortList | Gets the collection of properties by which the search results are ordered. You use the Add() method of the collection to add more properties. |
SortSimilar | Gets or sets a Boolean value that specifies whether query results based on similarity shall be sorted by similarity. |
StartRow | Gets or sets what is the first row included in the search results—that is, how many results to skip. |
TimeZone | |
TrimDuplicates | Gets or sets a Boolean value that specifies whether duplicate items should be removed from the search results. This property can also be used to collapse hits in the result set. Collapsing means to group items, whereas removing duplicates keeps one. |
TrimDuplicatesIncludeId | Specifies the value associated with a collapse group, typically used when a user clicks the Duplicates (n) link of an item with duplicates. This value corresponds to the value of the fcoid managed property that is returned in query results. See http://msdn.microsoft.com/en-us/library/ff521593.aspx for more information about duplicate removal and hit collapsing with FS4SP. |
TrimDuplicatesKeepCount | Gets or sets the number of duplicates to keep if duplicates are returned in the search results. Default value: 1 |
TrimDuplicatesOnProperty | Gets or sets the property that the duplicates trimming is based on. Default value: documentsignature |
UrlZone | Gets the originating URL zone for the query request. |
UserContextData | Gets or sets the user context data associated with the FS4SP search settings. |
UserContextGroupID | Gets or sets the identifier for getting the correct keyword settings, such as synonyms, best bets, and visual best bets. The ID is the Site Collection ID for the site collection where these keyword features have been configured. |
Find the KeywordQuery class in the Microsoft.Office.Server.Search.Query namespace. MSDN documentation about this class can be found at http://msdn.microsoft.com/en-us/library/microsoft.office.server.search.query.keywordquery.aspx.
Many properties are available with KeywordQuery, and it takes time to get to know them all. The following sample queries for FAST Search for SharePoint sorts the results with the newest items on top, and, if the dates are equal, sorts on rank. The sample includes best bets and visual best bets and includes refiners for last modified date and the format of the items. The sample also shows a second query, refining on Microsoft PowerPoint files, where the refiner is picked from the first query.
For the third query, you execute a Find Similar query by using the first result hit as the source for the query.
// Get a reference to the Query SSA Proxy var searchProxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy. GetProxy(SPServiceContext.Current); KeywordQuery kq = new KeywordQuery(searchProxy); kq.QueryText = "FAST Search for SharePoint"; kq.ResultsProvider = SearchProvider.FASTSearch; // Return search results, refiners and best bets kq.ResultTypes = ResultType.RelevantResults | ResultType.RefinementResults | ResultType. SpecialTermResults; // Add refiner for last modified date and file format // "write" is needed for exact TotalRowsIncludingDuplicates count kq.Refiners = "write,format"; // Set sorting to newest items on top kq.SortList.Add("write", SortDirection.Descending); // If dates are the same, sort on rank kq.SortList.Add("default", SortDirection.Descending); // Set the user context group in order to get Synonyms, Promotions, // Best Bets and Visual Best Bets kq.UserContextGroupID = SPContext.Current.Site.ID.ToString(); // Execute query ResultTableCollection resultTableCollection = kq.Execute(); // Get a reference to the search results ResultTable resultTable = resultTableCollection[ResultType.RelevantResults]; // Get the total count with and without duplicates from the search result table int totalRows = resultTable.TotalRows; int totalRowsIncludingDuplicates = resultTable.TotalRowsIncludingDuplicates; // Get the visual best bets ResultTable visualBestBetsTable = resultTableCollection[ResultType.VisualBestBetsResults]; // Pulls out and discards the values to show the retrieval process foreach (DataRow row in visualBestBetsTable.Table.Rows) { string name = (string)row["Name"]; string uri = (string)row["Uri"]; string description = (string)row["Description"]; string keyword = (string)row["Keyword"]; string teaser = (string)row["Teaser"]; string contentType = (string)row["TeaserContentType"]; } // Get a reference to the refiners ResultTable refinementTable = resultTableCollection[ResultType.RefinementResults]; // Find the PowerPoint refiner string powerPointRefinerToken = null; foreach (DataRow row in refinementTable.Table.Rows) { if ((string)row["RefinerName"] == "format" && (string)row["RefinementName"] == "Microsoft PowerPoint") { // Save the token for the PowerPoint refiner powerPointRefinerToken = (string)row["RefinementToken"]; break; } } // Add the PowerPoint refinement token to the query object if (!string.IsNullOrEmpty(powerPointRefinerToken)) kq.RefinementFilters.Add(powerPointRefinerToken); // Add docvector to be used in the subsequent find similar query kq.SelectProperties.Add("docvector"); // Execute the search one more time with refinement on "PowerPoint" files resultTableCollection = kq.Execute(); // Prepare a find similar query for the first hit resultTable = resultTableCollection[ResultType.RelevantResults]; string docvector = (string)resultTable.Table.Rows[0]["docvector"]; kq.SimilarType = SimilarType.Find; kq.SimilarTo = docvector; // Remove refinement for "PowerPoint" files kq.RefinementFilters.Clear(); // Execute the search one more time using find similar resultTableCollection = kq.Execute();
Make sure you set UserContextGroupID to the ID of your Search Settings Group when using the KeywordQuery class to return best bets and visual best bets. If you don’t, they will always be empty. See the section Keyword, Synonym, and Best Bet Management in Chapter 6 for more information about keywords and the Search Settings Group. In most cases, this property corresponds to the GUID of your site collection.
You can see other examples of querying with the Query Object Model at http://msdn.microsoft.com/en-us/library/ms551453.aspx and in Chapter 9.
The following sample code searches for the term FAST Search for SharePoint and gives 1,000 extra rank points to items created by the author Mikael. If the items also contain the names Marcus and Robert, an additional 2,000 rank points are given.
FQL formatted for readability
xrank( xrank( string( "FAST Search for SharePoint", mode="simpleall" ), author:string( "mikael" ), boost=1000 ), or( string( "marcus" ), string( "robert" ) ), boost=2000 )
KeywordQuery class executing an FQL query
// Get a reference to the Query SSA Proxy var searchProxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy. GetProxy(SPServiceContext.Current); KeywordQuery kq = new KeywordQuery(searchProxy); // Enable FQL kq.EnableFQL = true; // Add the FQL query kq.QueryText = "xrank(xrank(string("FAST Search for SharePoint", mode="simpleall"), author:s tring("mikael"),boost=1000), or(string("marcus"), string("robert")), boost=2000)"; kq.ResultsProvider = SearchProvider.FASTSearch; // Return search results kq.ResultTypes = ResultType.RelevantResults; // Execute search ResultTableCollection resultTableCollection = kq.Execute();
When you create applications that are hosted outside of SharePoint but you want the applications to be searchable within SharePoint, you use the Query Web Service. This service can be consumed by any programming language that can talk to a web service.
To access the Query Web Service and its methods, add a web service reference to the following URL, where server is your SharePoint web application URL and [site collection] is the root folder of your site collection.
http://server/[site collection/]_vti_bin/search.asmx
You can get the WSDL for the search Web Service by using the URL http://server/_vti_bin/search.asmx?wsdl.
The Query Web Service contains eight methods, listed in Table 8-14. The most important methods are Query, QueryEx, and GetSearchMetadata.
Table 8-14. Public methods available via the Query Web Service with FS4SP relevant settings
Description | |
---|---|
GetPortalSearchInfo | Returns a list of scopes for the SharePoint Server search. This method is deprecated and should not be used in new applications. |
GetQuerySuggestions | Returns suggested queries from the query history or from manually added entries. It is meant for assisting the user with suggested queries as he/she is typing into the search box and is typically invoked using JavaScript. |
GetSearchMetadata | Returns a list of all the managed properties and scopes in an ADO.NET DataSet. Table names:
|
Query | Returns an XML document containing the search results set for the specified query. This method cannot be used to return refiners, best bets, or visual best bets. |
QueryEx | Returns an ADO.NET DataSet containing a DataTable for each search results set returned for the specified query. Table names:
|
RecordClick | This member is reserved for internal use and is not intended to be used directly from your code. |
Registration | Returns the name and provider registration information for the search Shared Service Provider. |
Status | Returns availability of the search service. Returns a status of ONLINE or OFFLINE to indicate the availability of the search service. |
The schema for writing search queries by using the Query Web Service is described at http://msdn.microsoft.com/en-us/library/ms563775.aspx. The WSDL for the Query XML can be found at http://msdn.microsoft.com/en-us/library/dd955992(office.12).aspx.
The following sample builds a console application that uses the executing user’s domain credentials for authentication. The Query Web Service is added as a Web Service reference created with wsdl.exe (Microsoft Web Services Description Language Utility).
The sample queries for FAST Search for SharePoint sort the results with the newest items on top, includes best bets and visual best bets, and includes refiners for last modified date and the format of the items. The sample also shows a second query, refining on PowerPoint files, where the refiner is picked from the first query.
Note that if anonymous access is disabled for the Query Web Service’s web application, you have to provide user credentials on calls to the Query Web Service. The results returned from the Query Web Service are filtered according to what the executing user has rights to, similar to how search works inside of SharePoint.
using System; using System.Data; using System.Net; namespace FS4SP.ConsoleSamples { internal class Program { private static void Main(string[] args) { // The Query Packet XML template for our query string templateXml =@" <QueryPacket xmlns=""urn:Microsoft.Search.Query""> <Query> <Context> <QueryText language=""en-US"" type=""STRING"">FAST Search for SharePoint</QueryText> </Context> <SupportedFormats> <Format revision=""1"">urn:Microsoft.Search.Response.Document:Document</Format> </SupportedFormats> <ResultProvider>FASTSearch</ResultProvider> <Range> <StartAt>1</StartAt> <Count>10</Count> </Range> <EnableStemming>true</EnableStemming> <EnableSpellCheck>Off</EnableSpellCheck> <IncludeSpecialTermResults>true</IncludeSpecialTermResults> <IncludeRelevantResults>true</IncludeRelevantResults> <ImplicitAndBehavior>true</ImplicitAndBehavior> <TrimDuplicates>true</TrimDuplicates> <SortByProperties> <SortByProperty name=""write"" direction=""Descending"" /> </SortByProperties> <IncludeRefinementResults> <Refiners> <Refiner>format</Refiner> <Refiner>write</Refiner> </Refiners> </IncludeRefinementResults> {0} <Properties> <Property name=""Rank"" /> <Property name=""Title"" /> <Property name=""Author"" /> <Property name=""Size"" /> <Property name=""Path"" /> <Property name=""Write"" /> <Property name=""SiteName"" /> <Property name=""ContentClass"" /> <Property name=""IsDocument"" /> <Property name=""Url"" /> <Property name=""FileExtension"" /> <Property name=""SpSiteUrl"" /> <Property name=""docvector"" /> </Properties> </Query> </QueryPacket>"; // Accept all certificates - should only be used against sites you trust! ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; // Get an instance to the Web Service proxy // Generated with "wsdl.exe http://<server>/_vti_bin/search.asmx QueryService service = new QueryService(); // Set the SharePoint server URL service.Url = "http://test/_vti_bin/search.asmx"; // Set credentials for the executing user service.PreAuthenticate = true; service.UseDefaultCredentials = false; service.Credentials = CredentialCache.DefaultNetworkCredentials; // Create the Query Packet and execute the query string queryXml = string.Format(templateXml, string.Empty); DataSet resultSet = service.QueryEx(queryXml); // Get a reference to visual best bets and display the results DataTable visualBestBetsTable = resultSet.Tables["VisualBestBetsResults"]; if (visualBestBetsTable != null) { foreach (DataRow row in visualBestBetsTable.Rows) { Console.WriteLine(row["Name"]); Console.WriteLine(row["Uri"]); Console.WriteLine(row["Description"]); Console.WriteLine(row["Keyword"]); Console.WriteLine(row["Teaser"]); Console.WriteLine(row["TeaserContentType"]); } } // Get a reference to results and print properties and values DataTable resultTable = resultSet.Tables["RelevantResults"]; for (int i = 0; i < resultTable.Rows.Count; i++) { Console.WriteLine("RESULT #" + (i + 1)); DataRow row = resultTable.Rows[i]; for (int colIdx = 0; colIdx < row.ItemArray.Length; colIdx++) { Console.WriteLine(resultTable.Columns[colIdx].ColumnName + ":" + row.ItemArray[colIdx]); } Console.WriteLine("-------------------------"); } // Get a reference to the refiners DataTable refinementTable = resultSet.Tables["RefinementResults"]; // Find the PowerPoint refiner string powerPointRefinerToken = null; foreach (DataRow row in refinementTable.Rows) { if ((string)row["RefinerName"] == "format" && (string)row["RefinementName"] == "Microsoft PowerPoint") { // Save the token for the PowerPoint refiner powerPointRefinerToken = (string)row["RefinementToken"]; break; } } // Create the refinement filter for the query and add it to the Query Packet XML string refinementXml = "<RefinementFilters><RefinementFilter>" + powerPointRefinerToken + "</RefinementFilter></RefinementFilters>"; queryXml = string.Format(templateXml, refinementXml); // Execute the search one more time with refinement on "PowerPoint" files resultSet = service.QueryEx(queryXml); } } }
If you want to execute the query by using FQL instead of Keyword Query Syntax, replace the following line.
<QueryText language="en-US" type="STRING">FAST Search for SharePoint</QueryText>
Use the following line as the replacement.
<QueryText language="en-US" type="FQL">string("fast for sharepoint", Mode="AND")</QueryText>
SharePoint includes a search page that returns the result as RSS. This page accepts the same URL syntax as the Search Center in addition to unique parameters listed earlier in Table 8-6.
To access the RSS feed for a search result, use the following URL.
http://server/[site collection/]_layouts/srchrss.aspx
Executing a search for FAST Search for SharePoint on all sites against a site collection at the URL http://test (http://test/) would look like the following.
http://test/_layouts/srchrss.aspx?k=fast%20search%20for%20sharepoint&s=All%20Sites&start=1&prov ider=FASTSearch
Using RSS can be a good option for many programming languages because the format is standardized and most languages have libraries that make it easy to read and parse RSS feeds. The RSS feed viewed in Windows Internet Explorer 9 looks similar to that of Figure 8-5.
This chapter covered four different search APIs, all with different characteristics and each one useful for different scenarios. Table 8-15 summarizes the different APIs and the usage areas for each one.
Table 8-15. Feature set and usage area for the different query APIs
Search Center Web Part | Custom Web Part | Applications outside of SharePoint | FQL support | Refiners | Best bets and visual best bets | |
---|---|---|---|---|---|---|
Federated Search Object Model | X | X | X[a] | X[b] | X[c] | |
Query Object Model | X | X | X | X | ||
Query Web Service | X | X | X | X | ||
Query via RSS | X | |||||
[a] Available using reflection to get access to the FASTSearchRuntime. See Chapter 9 for an example on extending the Core Results Web Part to support FQL. [b] Via RefinementManager [c] Via FASTSearchRuntime |
As you see in this chapter, several APIs and two different query languages—KQL and FQL—can be used for searching against FS4SP. Knowing the strengths and limitations of each one is important when setting out to create your next search-driven application.
Whether you choose to customize the existing Search Center and its Web Parts or do a fully custom search application is up to you. Although the Search Center option makes it easy to get up and running, it limits how you can sort and rank results on multiple levels and limits you to only using KQL. Extending the Web Parts to support this functionality is often more work compared to using the KeywordQuery class directly. On the other hand, using the KeywordQuery class gives you full flexibility in terms of how you query the index by using FQL but leaves it up to you to create the user experience. You also lose the keyword functionality with synonyms and best bets unless you develop it yourself. Both options are valuable and cater to different needs and different skill sets when it comes to building the UI.
You have been given samples about how to query with the APIs using the Federated Search Object Model or the Query Object Model when developing applications hosted inside of SharePoint and using either the Query Web Service or query via RSS for applications hosted outside of SharePoint.
The goal of this chapter has been to give you an overview of the different query options available to you in FS4SP, and to explain how these options all tie together behind the scenes. The options available are almost similar to that of the built-in SharePoint search, but with provided extensions for FS4SP-specific features. Mastering the new feature set available with FS4SP when it comes to querying the search index takes time, but hopefully you have gained a better understanding of what is available and how you can tap into their potential.
18.118.137.243