Share via


Enterprise Search Query Object Model Overview

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:

  • A custom search Web Part hosted in a SharePoint site.

  • An ASPX Web application.

The Query object model supports both Enterprise Search SQL Syntax Reference and Enterprise Search Keyword Syntax Reference search queries.

Choosing Between the Query Object Model and the Query Web Service

Enterprise Search also exposes its search functionalities through the Query Web service. Like the Query object model, the Query Web service also support SQL and Keyword syntax queries, and both support returning multiple result types. The primary consideration when choosing between the two for custom applications is the location of the application. If the application is local to the server running Office SharePoint Server 2007, you can use the Query object model. For remote applications, you must use the Query Web service. For more information about the Query Web service, see Enterprise Search Query Web Service Overview.

Accessing the Query Object Model

To use the Query object model, you must set references to the following DLLs:

  • Microsoft.Office.Server.dll

  • Microsoft.Office.Server.Search.dll

  • Microsoft.SharePoint.dll

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, 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—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 might 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 by using 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 Services Provider (SSP) for the search service (as an instance of the ServerContext class).

  • Application name of the Shared Services Provider (SSP) for the search service (a string with the SSP'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);

Reading the Search Results

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);

Result Types

The search results returned by Enterprise Search are grouped into four different result types, as described in the following table.

Name Description

RelevantResults

The main result set, containing search results from the content index that match the query.

HighConfidenceResults

The result set containing high-confidence results.

SpecialTermResults

The result set containing best bets matching the query.

DefinitionResults

The result set containing definitions for keywords matching the query.

These result types are defined in the ResultType enumeration of the Microsoft.Office.Server.Search.Query namespace.

See Also

Reference

Enterprise Search Keyword Syntax Reference

Concepts

Enterprise Search SQL Syntax Reference
Building Custom Enterprise Search Web Parts
Enterprise Search Query Web Service Overview