Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Full Text Search using CONTAINS, FORMSOF, NEAR for multiple search words

I am new to SQL Server Full Text Searching, and am trying to figure out the best way to search on multiple words using the inflectional engine so the search uses the various forms of all of the words.

From what I read, FREETEXT uses an implicit OR when used with multiple words. I want an AND so that the search results contain all of the words, so because of this I am choosing to use CONTAINS.

I am trying to do something like the query below, which uses FORMSOF with the proximity keyword NEAR for multiple words. Note that this is not valid syntax and returns an error:

select top 5 *
from content
WHERE CONTAINS((Title,Subtitle,Body), 'FORMSOF(INFLECTIONAL, model NEAR airplane)')

However, the query below works, but I don't know if it gives the intended results. Is there a difference between "AND" and "NEAR" with SQL Full Text Search?

select top 5 *
from content
WHERE CONTAINS((Title,Subtitle,Body), 'FORMSOF(INFLECTIONAL, model) AND FORMSOF(INFLECTIONAL, airplane)')

I guess what I am asking is, is there a way to use CONTAINS, FORMSOF, and NEAR with multiple search words? Or should I just use the second query above that uses "AND"?

like image 927
n3wton23 Avatar asked Jan 18 '11 16:01

n3wton23


People also ask

How does full text search work in SQL Server?

Full-text queries perform linguistic searches against text data in full-text indexes by operating on words and phrases based on the rules of a particular language such as English or Japanese. Full-text queries can include simple words and phrases or multiple forms of a word or phrase.

How do you do multiple search in SQL?

Yes, you can use SQL IN operator to search multiple absolute values: SELECT name FROM products WHERE name IN ( 'Value1', 'Value2', ... );

How do I use full text search?

Implement full-text search in SQL To implement a full-text search in a SQL database, you must create a full-text index on each column you want to be indexed. In MySQL, this would be done with the FULLTEXT keyword. Then you will be able to query the database using MATCH and AGAINST.

How do I enable full text search in SQL Server?

If you are going to install a new SQL Server instance, then you don't need to do much except click the checkbox near "Full-Text and Semantic Extractions for Search" option on the feature selection window in the SQL Server setup. This feature will be installed along with other components on your server.


1 Answers

From the docs:

<proximity_term> ::= 
     { <simple_term> | <prefix_term> } 
     { { NEAR | ~ }
     { <simple_term> | <prefix_term> } 
     } [ ...n ] 

This means you can use NEAR predicate for (possible prefixed) words, phrases and their combinations.

Since your search terms are inflected using quite simple rules, you can just use prefixes:

SELECT  *
FROM    content
WHERE   CONTAINS((Title,Subtitle,Body), 'model* NEAR airplane*')

or use AND and do fine filtering on the client side

SELECT  *
FROM    ft
WHERE   CONTAINS((Title,Subtitle,Body), 'FORMSOF(INFLECTIONAL, "model") AND FORMSOF(INFLECTIONAL, "airplane")')
like image 132
Quassnoi Avatar answered Sep 23 '22 05:09

Quassnoi