Keyword Query in SharePoint Search

Keyword Query in SharePoint Search
5 votes, 3.00 avg. rating (65% score)

Introduction

Most of the user are not often familiar with the special keyword query syntax supported by SharePoint. Keyword query syntax allows users to enter keywords, phrases, and Managed Property names to create sophisticated searches. In addition, keyword query syntax supports operations and wildcards.
In this post we will cover Keyword Query in SharePoint Search.
The post includes

  • Keyword Query samples
  • Using Keyword Query class in getting search results

Skill Level – Medium

Keyword Query

The simplest form of keyword query is to enter a single term, without spaces or punctuation (for example, Training). This form will cause SharePoint to search both the index and the Metadata Properties, returning all matching results. A more sophisticated search would involve multiple keywords, but these must be enclosed in quotations (for example, “Training Materials”).
Required and excluded terms may be added to the query using plus (+) and minus (-) signs. For example, the following query returns results for the term “business” except when it is used in the phrase “Business Connectivity Services” or “Business Intelligence.”

business – “Business Connectivity Services” -“Business Intelligence”

Operators may also be used with keywords. This includes Boolean operators, wildcards, and arithmetic. Boolean operations are done using the AND and OR operators. Wildcards are supported via an asterisk (*). The following examples show a query with a Boolean operator.

“Business Connectivity Services” OR “Business Intelligence”

Managed Properties may also be used as filters for keyword queries. This allows you to create queries that use the full power of the keyword query syntax against specific fields. Check the below section for Keyword Query Samples for usring managed properties with filters

Keyword Query Samples

Lastname:A*
Returns people whose last name starts with A

AverageRating>0
Returns items whose average rating is greater than zero

Training +isDocument:1
Returns documents that are related to training

Client AND Server
Returns items containing both “Client” and “Server”

Using Keyword Query class in getting search results

Along with using keyword query syntax in Search Center, developers may also create custom search Web Parts based on keyword queries. The KeywordQuery class contains the functionality necessary to issue keyword queries programmatically. The KeywordQuery class contains several properties for preparing the query and an Execute method to run the query. The query results are returned as a ResultTableCollection, which contains a collection of IDataReader objects.
Basic code using the KeywordQuery class


SearchServiceApplicationProxy proxy =   (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(
                       SPServiceContext.GetContext(SPContext.Current.Site));

KeywordQuery keywordQuery = new KeywordQuery(proxy);
keywordQuery.ResultsProvider = SearchProvider.Default;
keywordQuery.ResultTypes = ResultType.RelevantResults;
keywordQuery.EnableStemming = false;
keywordQuery.TrimDuplicates = true;
keywordQuery.QueryText = query;
ResultTableCollection results = keywordQuery.Execute;
ResultTable result = results[ResultType.RelevantResults];

DataTable table = new DataTable;
table.Load(result, LoadOption.OverwriteChanges);
myGrid.DataSource = table;
myGrid.DataBind;

Conclusion

To use the KeywordQuery class, you must create an instance that references the SSA proxy. Once the class is created, then you can set the QueryText property with keyword query syntax. Additional properties, such as TrimDuplicates and EnableStemming, allow finer control over the query. When the results are returned, you may process them manually or bind them directly to a control

December 7, 2013 · Adi · No Comments
Tags: ,  · Posted in: C#, Search, Sharepoint 2010, SharePoint 2013

Leave a Reply

What is 4 + 4 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)