Export (0) Print
Expand All

Getting Started with the Enterprise Search Query Object Model 

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Enterprise Search in Microsoft Office SharePoint Server 2007 provides a new Query object model that you can use in custom search Web Parts and search applications to execute queries against the Enterprise Search service.

The Query object model is implemented in the Microsoft.Office.Server.Search.Query namespace, found in the Microsoft.Office.Server.Search.dll.

Using the Query Object Model in Custom Search Applications

You can write code that uses the Query object model from different types of applications, including from the following:

  • A custom search Web Part hosted in a SharePoint site

  • An ASPX Web application

  • A Windows Forms client application

To access the classes in the Microsoft.Office.Server.Search.Query namespace, you must include a reference in your project to the Microsoft.Office.Server.Search.dll. Depending on how you implement the code that executes the search query, you may also need to reference the following:

  • Microsoft.Office.Server.dll

  • Microsoft.SharePoint.dll

Within your application's code, you include the using namespace directive for Microsoft.Office.Server.Search.Query. Following is the syntax to use:

using Microsoft.Office.Server.Search.Query;

About the Query Classes

The Microsoft.Office.Server.Search.Query namespace includes three Query classes:

  • Query  This class is not intended to be used directly from your code, but is designed to be the base implementation for the Enterprise Search Query object model classes. In your code you should use the FullTextSqlQuery and KeywordQuery classes.

  • FullTextSqlQuery  Use this class to execute SQL syntax search queries.

  • KeywordQuery  Use this class to execute keyword syntax search queries.

Choosing a Query Class for a Custom Search Application

To determine the appropriate class to use for your custom search application—FullTextSqlQuery or KeywordQuery—you should consider the level of complexity in the search queries that you want your application code to support.

If you can create the queries you need using only keyword syntax, you may prefer to use the KeywordQuery class. If you use keyword syntax for your search queries, you can pass the search terms directly to the search component and do not need to parse through the search terms to build the query. As a result, the process of constructing queries using keyword syntax is simple.

However, if you need to construct more complex queries, keyword syntax may not work. For these queries, you should use the FullTextSqlQuery class. For example, keyword syntax supports only phrase, word, or prefix exact matches. Also, while you can use keyword syntax to specify whether to include or exclude a particular search term, you cannot construct complex groupings of included and excluded terms. You can accomplish this with FullTextSqlQuery.

The following list identifies additional query elements that are supported only with SQL search syntax using the FullTextSqlQuery class:

  • FREETEXT()

  • CONTAINS()

  • LIKE

  • ORDER BY

Executing the Search Query

The flow of execution of the KeywordQuery class and FullTextSqlQuery class is essentially the same. The constructor for the class has three overloads; when instantiating the class, you need to specify one of the following:

  • Site collection (as an instance of the SPSite class)

  • Context of the Shared Service Provider for the search service (as an instance of the ServerContext class)

  • Application name of the Shared Service Provider for the search service (a string with the Shared Service Provider’s GUID)

The following code example shows how to use the KeywordQuery class:

using Microsoft.SharePoint;
...
KeywordQuery qRequest = new KeywordQuery(new SPSite("http://*****);

The following code example shows how to use the FullTextSqlQuery class:

using Microsoft.Office.Server;
...
FullTextSqlQuery qRequest = new FullTextSqlQuery(ServerContext.Current);

The ResultTable class implements the IDataReader Interface so that you can easily access the result rows from your code.

Following is an example:

ResultTableCollection resultTables = queryRequest.Execute();
ResultTable relevantResults = resultTables[ResultType.RelevantResults];
DataTable resultsDataTable = new DataTable();
resultsDataTable.Load(relevantResults,LoadOption.OverwriteChanges);
Show:
© 2014 Microsoft