views:

207

answers:

4

Hello, I was wondering if SQL Server is actually utilizing its fulltext index on searches that can be in the middle of text (such as LIKE %searchKeyword%)?

I forgot where I read it, but I did read somewhere that the fulltext index would only be utilized when it is used in a "StartsWith" manner (searchKeyword%) where you search for a LIKE with a fixed beginning and a variable ending.

Please let me know if this is true?

A: 

true, it works with keyword in middle of a text also. for example the link below uses full text search..

and the word men is found in middle of their contents. hope it helps.

http://www.catalogues4u.com.au/Search.aspx?keyword=men

Aneef
Sorry, this is simply not true - see my post and read the two documentation links. SQL Server fulltext does not support searching for %men% or *men*
marc_s
I guess the question is not using %men% as keyword. its about a keyword being inside a paragraph or phrase. correct me if im wrong
Aneef
+1  A: 

Full-text indexes (if you have them, they aren't enabled by default) are accessed using functions like CONTAINS() and FREETEXT(), not the LIKE operator.

Normal indexes (e.g., "CREATE INDEX ix_tbl_fld ON mytable(textfield)") can be used by the LIKE operator, but not when the pattern begins with a % wildcard... these searches require a scan of all of the values in the table.

If you have a lot of rows with the same value repeated, using table compression under SQL Server 2008 may improve performance of LIKE '%keyword%' searches, even without an index (I don't have it, so I can't test this theory...).

richardtallent
How would you access Contains or Freetext through a Linq Query? I've been using "StartsWith" (like the string function) which turns into a LIKE %% query when realized. How do I access the freetext index methods you describe from C#/LINQ?
Alex
Sorry, I avoid LINQ, for exactly this sort of reason--too leaky an abstraction for my taste.
richardtallent
A: 

If you read the MSDN documentation carefully, you'll see you can only search for fixed expressions like " hockey ", or for prefix-based expressions, like " hockey* " - but not for anything else, unfortunately.

See:

Supported Forms of Query Terms

Performing prefix searches

Unfortunately, that's pretty much all there is. You cannot search for " hockey " :-(

Marc

marc_s
A: 

The best way to do free-text search in SQL Server, that gives you not only a powerful search* but also returns your results ranked, is to use FreeTextTable.

eg:

SELECT * 
FROM FreeTextTable(YourTableName,(Column1,Column2), 'search term')
INNER JOIN YourTableName as T ON T.ID = [Key] ORDER BY RANK DESC

This will give you ranked results in order of how near they matched (via the RANK column).

  • *"Any text, including words, phrases or sentences, can be entered. Matches are generated if any term or the forms of any term is found in the full-text index.... freetext_string is wordbroken, stemmed, and passed through the thesaurus. If freetext_string is enclosed in double quotation marks, a phrase match is instead performed; stemming and thesaurus are not performed."*
Dan Diplo