3

I am trying to query the Windows Search index programmatically using the Windows Search SQL Syntax

I am able to search file contents in my C# winforms application using the search query below:

string strSearchQuery =
    "SELECT System.ItemName FROM SystemIndex " +
    "WHERE scope ='file:" + @"C:\myfolder\" + 
    "' and FREETEXT('dummy')";

ISSUE: However, I am not able to use this query to search for part of a word in the file contents.

For example: If a file (such as a .txt file) contains the the text "abcd", and I search for FREETEXT('ab'), it doesn't show that file.

I have tried using:

  • FREETEXT('ab')
  • FREETEXT('*ab*')
  • FREETEXT('\"ab\"')
  • FREETEXT('\"*ab*\"')
  • FREETEXT('*\"ab\"*')
  • FREETEXT('ab*')
  • FREETEXT('\"*ab*\"')
  • FREETEXT('\"ab\"*')

I've also tried using CONTAINS with the above combinations instead of FREETEXT.

When I search for ab directly on Windows search, it shows the file having the text

How can I modify this query to search for a part of a word in the file contents? Please help!

slayernoah
  • 4,382
  • 11
  • 42
  • 73

3 Answers3

3

FREETEXT() does not support wild cards. Use CONTAINS() instead.

string query = 
   "SELECT System.ItemName, System.ItemUrl FROM SystemIndex " +
   "WHERE scope ='file:D:\MyFolder' and CONTAINS('\"dumm*\"')";

Note that the partial word you want to search on must be contained within double quotes, along with the wildcard * character.

0

The wildcard character is the %, so your query should look like this :

string strSearchQuery =
    "SELECT System.ItemName FROM SystemIndex " +
    "WHERE scope ='file:" + @"C:\myfolder\" + 
    "' and FREETEXT(%'dummy'%)";

EDIT :

 string strSearchQuery =
        "SELECT System.ItemName FROM SystemIndex " +
        "WHERE scope ='file:" + @"C:\myfolder\" + 
        "' and FREETEXT('%dummy%')";
Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
0

There's a COP_VALUE_CONTAINS operator. The SQL symbol is ~= or ~~. E.g. System.FullText :~="abc".

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46