Sql-server – SQL Server Specific term full text search is not working

cfull-text-searchsql server

I'm trying to perform an specific term full-text search in SQL Server. It looks like the way to do it is by wrapping the keywords in double quotes, which I'm doing in my app:

Query = "\"" + model.SearchString + "\""

However, when I perform the query against the database with a term such as "foo bar", it returns every occurrence of "foo" and "bar" inside the database. The expected result is that the database should return only the records that have "foo bar" specifically. This is the query:

SELECT o.OpinionDocumentId
FROM CONTAINSTABLE(OpinionDocuments, Html, '"foo bar"') AS KEY_TBL
LEFT JOIN dbo.OpinionDocuments AS o ON o.OpinionDocumentId = KEY_TBL.[KEY]
WHERE o.Date BETWEEN @from AND @to

What could be the reason why this query is not returning the results from an specific set of keywords?

Thanks

Best Answer

Your query actually needs to use two properties of the CONTAINSTABLE function-

<maximum distance> and <match_order>.

Using

CONTAINSTABLE(OpinionDocuments, HTML, 'NEAR((Foo, bar), 0, TRUE)') AS KEY_TBL

Specifies to find "Foo" near "Bar" with 0 search objects between (<maximum distance> ", 0,") and in the order specified (<match_order> ",TRUE")