Hail to Integrated Search!
Hey guys,
I wanted to share my experience with Windows Desktop Search 3.0 (WDS). It is really a fascinating technology, particularly from a developer point of view.
WDS 3.0 is built into Windows Vista and can be installed on Windows XP/2003. In Vista, it is directly tied into Windows Explorer. Besides basic searching (i.e. find me files that contain XYZ), it can do property based searching. The 'modified' date is a property that all files in Windows Vista have, so if I want to find all files that were modified in 2006, I can just type the following:
The search in Windows Explorer is instant, so you don't hit return, just type your query and if there are results, they will show up.
I've modified a lot of files in 2006 :) so I'll get way too many results. I can narrow down my search and look for only C# files that were modified in 2006.
If for some reason I wanted to be even more specific, I could further narrow my search to find only C# files modified in 2006 that are bigger that 10KB
Now that's all well and fine, I'm sure the Windows developers worked really hard to build in that functionality. But how hard would it be for a developer outside of Microsoft to build the same thing.
As it turns out, it is really quite simple.
Windows Search is exposed as an OLE DB data source; so all you really need to know is the connection string that it uses. As it turns out, the connection string is just:
"Provider=Search.CollatorDSO;Extended Properties='Application=Windows'"
So I can create an OLE DB connection as follows:
Windows Search understands a subset of T-SQL, however, all of the fancy property based stuff that we did in Windows Explorer (modified:2006, size > 10KB, etc) has to be translated into that query syntax.
Luckily, the Search API has a helper object do this translation for us. The SEARCHAPI.TLB for this API is .NET friendly, so we can just use tlbimp.exe to create a wrapper for us. The .TLB comes with the Platform SDK, and can be found in the Lib directory.
The SearchAPI has a simple 'GenerateSQLFromUserQuery' method that takes our input (i.e. modified:2006) and translates that into SQL that Windows Search understands.
For example, GenerateSQLFromUserQuery() turns 'modified:2006' into:
SELECT "System.ItemUrl",
"System.DateModified"FROM "SystemIndex"..scope()
WHERE (("System.DateModified" >= '2006/01/01 08:00:00' AND
"System.DateModified" < '2007/01/01 08:00:00'))"
All in all, it takes about 30 lines of code to integrate search into your application the same way that Windows Explorer does it. I posted my sample project here if anyone is interested.
Thanks!
Eric.




1 Comments:
Hi,
I'm happy to see that it's easy to integrate Desktop Search into .NET applications. Your sample code is missing (404 on link) however and I would be very interested in getting a copy. Thanks!
March 7, 2007 1:37 PM
Post a Comment
Links to this post:
<< Home