Using ISearchQueryHelper from managed code

ISearchQueryHelper is a COM interface that can be used to convert from Advanced Query Syntax (AQS) to a SQL query that can be passed to the OLE DB Provider for Windows Search. (For background, see my previous post: Advanced Query Syntax: What, Where, Why, and How)

Good news for managed code developers - there's a type library from which you can create an interop assembly (using the tlbimp.exe tool). You'll find the type library (it's called SearchAPI.tlb) in the Lib folder of the Windows SDK. 

Once you've generated an interop assembly and have added a reference to it from your managed code project, here are the necessary steps to convert from AQS to SQL syntax:

  1. Instantiate the Search Manager
  2. Get the catalog manager for the system index
  3. Query the catalog manager for its corresponding query helper
  4. Convert an AQS string to a SQL query string

Here's some C# code that shows how to convert an AQS string to a SQL query string (the code fragment assumes that aqsQuery is a string variable that contains an AQS query):

using SearchAPILib;


CSearchManager manager = new CSearchManagerClass();
CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");
CSearchQueryHelper queryHelper = catalogManager.GetQueryHelper();

string sqlQuery = queryHelper.GenerateSQLFromUserQuery(aqsQuery);