Page 7 of 7
It is worth knowing that you can control where a search occurs in a much more efficient manner than simply writing a condition on System.ItemPathDisplay which would simply search everywhere but only return documents that had a path that matched the specification.
If you use the SCOPE or the DIRECTORY predicates you can control where the search is performed. The difference between the two is that SCOPE does a deep search, i.e. including all subfolders, but DIRECTORY does a shallow search, i.e. just the folder you specify. For example:
searches C:/Files/Reports and all the subfolders it contains whereas:
searches just C:\Files\Reports.
Notice that SCOPE and DIRECTORY don't work as described in the documentation. There is no need to put "//" after "file:" and you can use either "/" or "\" in the path specification.
The real power of the search engine is that it indexes the contents of a wide range of files. If you want to search the contents of indexed files you need to know about FREETEXT and CONTAINS. FREETEXT is the simpler of the two and it enables you to search for a fixed phrase. For example:
searches for any file that contains "iProgrammer".
You can also add a column specification so that properties are searched along with content. For example to search all of the properties and content use:
FREETEXT uses a variety of matching algorithms and will return a measure of how well the document matches the search criteria in System.Search.Rank which you can retrieve and use. You can modify the way the rank is calculated using the RANK BY clause – see the documentation for more information.
The CONTAINS predicate is more suited to looking for exact matches. You can specify what you are looking for using AND, OR and NOT and use wildcards. For example, to find any word starting with "iProg" you would use:
You can also use NEAR to find occurrences of one word near another, FORMSOF to match for words and inflections of the word. For example to find documents containing run, ran or running you would use:
Full text searches are clearly powerful but a programmed search can generally do more by including conditions on properties such as System.Author or System.DateCreated.
The Desktop Search facility is very powerful and we have only just scratched the surface but we have looked at a little of almost every aspect of its operation and proved that databases are easy. You should now find it possible to follow the documentation provided on the Microsoft website and create custom search applications that do exactly what you need.
To access the code for this project, once you have registered, click on CodeBin.