次の方法で共有


Windows SharePoint Services Search Query Object Model

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Search in Windows SharePoint Services provides a new Query object model that you can use in custom search Web Parts and search applications to execute queries against the Search service.

The Query object model is implemented in the Microsoft.SharePoint.Search.Query namespace, found in the Microsoft.SharePoint.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 Windows SharePoint Services Search SQL Syntax and Windows SharePoint Services Search Keyword Syntax search queries.

Choosing Between the Query Object Model and the Query Web Service

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 custom application is local to the server running Windows SharePoint Services 3.0, 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 Windows SharePoint Services Query Web Service.

Accessing the Query Object Model

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

  • Microsoft.SharePoint.Search.dll

  • Microsoft.SharePoint.dll

About the Query Classes

The Microsoft.SharePoint.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 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 two overloads; when instantiating the class, you need to specify one of the following:

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

  • Site name

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

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

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

There is only one result type returned by Search in Windows SharePoint Services, relevant results.

See Also

Reference

Windows SharePoint Services Search Keyword Syntax

Concepts

Windows SharePoint Services Search SQL Syntax

Windows SharePoint Services Query Web Service