Tuesday, September 27, 2011

SharePoint 2010 Search Query Suggestions Explained

Technorati Tags: ,,

A principle of interface design is to provide the user with feedback about the status of the system and how that relates to the user's interactions with the system. Searching can be mentally intensive and feedback about query formulation, such as the reasons the particular results were retrieved, and what next steps can be taken can help the user easily find what they are searching for. One of the ways SharePoint Search gives feedback is the feature called “Query Suggestions”.  You are probably familiar with this feature if you have ever searched using Google or Bing.

 

SharePoint query suggestions come in two forms, pre-query and post-query.

  • A pre-query suggestion is type-ahead feature that appears under the search box as shown above.

  • A post-query suggestion is similar except that it appears after the search has been submitted. It appears as a “Did you mean” link; by clicking it, the query is then executed and the results displayed.

 

Both types of query suggestions adhere to the search UI best practice of helping users search efficiently.

 

What determines a query suggestion?

Query suggestions are based on frequently used search terms and click through to results. Listed below are the steps needed for SharePoint to promote a term to a query suggestion.

  1. The associated Search Service application must have query logging enabled.
  2. The user must type in a valid word or set of words in the search box.
  3. The user must click on 6 or more search result links and open or save the file. Some types of pages containing the term can qualify as click-throughs.
  4. The Query Logging timer job must be run.
  5. The Prepare Query Suggestions time job must be run.

You can also through Microsoft Powershell promote query suggestions which will make them appear immediately.

#Execute in SP2010 PowerShell console

#Get the name of the Search Service Application

$searchapp = get-SPEnterpriseSearchServiceApplication

#Add your query suggestion

New-SpEnterpriseSearchLanguageResourcePhrase-

SearchApplication $searchapp –Language EN-US –Type

QuerySuggestionAlwaysSuggest –Name “Document”

#Run the timer job

Start-SPTimerJob –Identity “Prepare Query Suggestions”

 

Enabling query suggestions

To enable pre-query suggestions edit the “Search Box” web part, and in the “Query Suggestions” section check the box “Show Query Suggestions”.  You can also adjust the delay in milliseconds before the drop down list appears and you can limit the number of suggestions shown.

 

To enable post-query suggestions navigate to your results page and edit the “Search Box” following the same instructions as before.

Adding query suggestions to your own search applications

The “Search Box” web part makes an ajax call to the Search.asmx web service’s GetQuerySuggestions method passing in the term on each keystroke. Your own search applications can do the same using jquery. You may want to do this from the server side object model. This can be accomplished using the KeywordQuery class in Microsoft.Office.Server.Search. Below is an example.

public static StringCollection GetQuerySuggestions(string term)
{

    StringCollection suggestions = null;

    SPServiceContext context =
                SPServiceContext.GetContext(
                SPServiceApplicationProxyGroup.Default,
                SPSiteSubscriptionIdentifier.Default);

    SearchServiceApplicationProxy searchProxy = context.
        GetDefaultProxy(typeof(SearchServiceApplicationProxy))
        as SearchServiceApplicationProxy;

    using (KeywordQuery query = new KeywordQuery(searchProxy))
    {
        query.QueryText = term;
        suggestions = query.GetQuerySuggestions(5, true, false, true);
    }

    return suggestions;
}  

The GetQuerySuggestions method takes four arguments. The first is the the maximum number of suggestions you want to return. The second is whether the suggestions are for the term the user is typing in the search box or for a completed query. If you set this to false you must set the KeywordQuery.QueryProperties.QueryText. This tells the code that you want post-query suggestions. The third argument tells the code that you want html encoded strings bolding the parts of the term that the user typed in. The last argument sets whether the suggestions should be capitalized.

Suggesting a best practice

SharePoint Search provides Search UI components and a rich set of Search Web Parts that enable many configuration options. Many of these options relate to Search UI best practices. Query suggestions provide informative and efficient feedback. Understanding the best practices helps you make the correct decisions when customizing the Search UI and ultimately making your ECM implementation much more “findable”.