« Live Maps launches new version with Firefox support for 3D | Main | A letter from Pandora founder - Help Save Pandora and Internet Radio »

Use Windows Desktop Search API Inside of Managed Code

Starting from Windows Desktop Search (WDS) 3.0, a new helper API ISearchQueryHelper is added to help simplify the construction of index OLE DB connection string and search queries. The search service is implemented and the API is exposed as COM objects. Fortunately, there is a way for managed code to invoke the WDS too.
Download the Windows Search SDK. After unzip the package, the search interop assembly (Microsoft.Search.Interop.dll) is in the "Managed" folder. Add it to the reference inside of your solution and put “using Microsoft.Search.Interop; ” at the top of your C# code. Now it’s ready to write C# code and make use of WDS API. Some of the unmanaged and managed counterparts of classes are the following:
Unmanaged
Managed
ISearchManager
CSearchManager
ISearchCatalogManager
CSearchCatalogManager
ISearchQueryHelper
CSearchQueryHelper
 
The following sample code constructs a search query using ISearchQueryHelper to display the sender, recipients, and summary of the first 100 emails that contain a particular keyword (“share” in this example).
int maxSumLength = 100;
 
// Setup the catalog and search query helper
CSearchManager srchMngr = new CSearchManager();
CSearchCatalogManager srchCatMngr = srchMngr.GetCatalog("SystemIndex");
CSearchQueryHelper srchQueryHelper = srchCatMngr.GetQueryHelper();
 
// Assemble the query
srchQueryHelper.QuerySelectColumns = "System.Message.FromAddress, System.Message.ToAddress, System.Search.AutoSummary";
srchQueryHelper.QueryWhereRestrictions = "AND CONTAINS(System.Kind, '\"email\"')";
string sqlQuery = srchQueryHelper.GenerateSQLFromUserQuery("share");
 
// Setup the OLE DB connection
OleDbConnection conn = new OleDbConnection(srchQueryHelper.ConnectionString);
conn.Open();
 
// Execute the query
OleDbCommand cmd = new OleDbCommand(sqlQuery, conn);
OleDbDataReader srchResult = cmd.ExecuteReader();
 
// Process the search result and send to output
for (int i = 0; (i < 100) && srchResult.Read(); ++i)
{
                string fromAddr = "";
                string toAddr = "";
                string sumAddr = "";
 
                if (null != srchResult.GetValue(0) && !(srchResult.GetValue(0) is System.DBNull))
                {
                    string[] addrs = (string[])srchResult.GetValue(0);
                    fromAddr = "From: " + System.String.Join(",", addrs);
                }
                if (null != srchResult.GetValue(1) && !(srchResult.GetValue(1) is System.DBNull))
                {
                    string[] addrs = (string[])srchResult.GetValue(1);
                    toAddr = "To: " + System.String.Join(",", addrs);
                }
                if (null != srchResult.GetValue(2) && !(srchResult.GetValue(2) is System.DBNull))
                {
                    sumAddr = (string)srchResult.GetString(2);
                }
 
                textBox1.AppendText("(" + i + ") ");
                textBox1.AppendText(fromAddr + " " + toAddr + " ");
                textBox1.AppendText("Content: " + (sumAddr.Length <= maxSumLength ? sumAddr : sumAddr.Substring(0, maxSumLength)));
                textBox1.AppendText(Environment.NewLine);
}
 
srchResult.Close();
conn.Close();
           

TrackBack

TrackBack URL for this entry:
http://stanblog.jojoyao.com/cgi-bin/mt/mt-tb.cgi/39

Post a comment


About

This page contains a single entry from the blog posted on April 17, 2007 7:10 AM.

The previous post in this blog was Live Maps launches new version with Firefox support for 3D.

The next post in this blog is A letter from Pandora founder - Help Save Pandora and Internet Radio.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33