Поделиться через


Querying the Index Programmatically

Windows Search offers a variety of avenues for searching the Index programmatically:

  • Using OLE DB
  • Using ISearchQueryHelper
  • Using Windows Search SQL Syntax
  • Using the search-ms Protocol
  • Using Advanced Query Syntax
  • Querying Remotely
  • Related Topics

Note  Microsoft Windows Desktop Search (WDS) 2x compatibility note: In Windows Search on XP and Vista, ISearchDesktop is deprecated. Instead, developers should use ISearchQueryHelper to get a connection string and to parse the user's query into Structured Query Language (SQL), then query via OLE DB.

Using OLE DB

OLE DB (Object Linking and Embedding Database) is a Component Object Model (COM) API that enables you to access different types of data stores uniformly, including non-relational databases like spreadsheets. OLE DB separates the data store from the application accessing it through a set of abstractions that include the datasource, session, command and rowsets. An OLE DB provider is a software component that implements the OLE DB interface in order to provide data from a given data store to other applications.

The Windows Search OLE DB provider is read-only, supporting only SELECT statements. It does not support INSERT and DELETE statements. You can access the Windows Search OLE DB provider programmatically using the OleDbConnection and OleDbSession objects in C# and using IDBInitialize, IDBProperties and other OLE DB interfaces in C++. The only property that has to be set, as can be seen in the following code samples, is the provider string to "Application=Windows". You can get the connection string for the Windows Search OLE DB provider by calling the ISearchQueryHelper::get_ConnectionString method. This is the preferred way to get the connection string.

For more information on OLE DB, please refer to OLE DB Programming Overview. For information on the .NET Framework Data Provider for OLE DB, refer to System.Data.OleDb namespace documentation.

Using ISearchQueryHelper

You can develop a component or helper class to query the index using the ISearchQueryHelper interface. This interface is implemented as a helper class to ICatalogSearchManager and can only be obtained by calling ISearchCatalogManager::GetQueryHelper. This interface helps you:

  • Obtain an OLE DB connection string to connect to the Windows Search database.
  • Convert user queries to Windows Search SQL.

Please refer to Using the ISearchQueryHelper Interface for more information on implementing the ISearchQueryHelper interface or to ISearchQueryHelper for complete documentation on the interface itself.

Using Windows Search SQL Syntax

The Windows Search SQL query language used by Windows Search extends the standard SQL-92 and SQL-99 database query syntax to enhance its usefulness with text-based searches. All features of Windows Search SQL are compatible with Windows Search on both Microsoft Windows Vista and Windows XP.

Please refer to Overview of the Search SQL Syntax for detailed information on the Windows Search SQL syntax.

Using the search-ms Protocol

The search-ms protocol is a convention for querying the Windows Search index. It is a flexible protocol that allows queries to be built with parameter-value arguments, including property arguments, previously saved searches, Advanced Query Syntax, Natural Query Syntax, and locale identifiers for both the Indexer and the query itself.

Please refer to Using the search-ms Protocol for detailed information on the search-ms protocol syntax.

Using Advanced Query Syntax

The Advanced Query Syntax (AQS) is used by Windows Search to help users and programmers better define and narrow their searches. Using AQS is an easy way to narrow searches and deliver better result sets. Searches can be narrowed by the following parameters:

  • File kinds: folders, documents, presentations, pictures and so on.
  • File stores: specific databases and locations.
  • File properties: size, date, title and so on.
  • File contents: keywords like "project deliverables," "AQS," "blue suede shoes," and so on.

Please refer to Advanced Query Syntax for detailed information on the AQS syntax.

Querying Remotely

On Vista, you can query the local index of a remote machine. First, you must share the folders you want available for remote access. In Windows Explorer, you can right-click the folder(s) and select "Sharing and security" to change the sharing permissions. After you share the folder(s), you can query the local index by specifying the remote machine's machinename before SYSTEMINDEX in the GetCatalog call (e.g., MyMachine.SYSTEMINDEX).