What are they and how is the syntax, to build data catalogs in SQL-Server.
FULL TEXT SEARCH
They are used in large text fields using indexes called catalogs. These catalogs can only be used with tables that have primary keys defined and store all the words in the table’s content except for articles, prepositions, etc.
Catalogs are not automatically updated or saved with the database, and each table can have only one catalog.
For the use of these catalogs within a query, we can use two methods, the first consists of including the criteria within a WHERE clause (CONTAINS or FREETEXT) and the second is using a temporary table that contains the hit ratio in the query. (CONTAINSTABLE or FREETEXTTABLE).
The CONTAINS predicate
This predicate is used to search for specific text in a table. Its operation is similar to the LIKE predicate, except that it cannot perform searches in large text fields. CONTAINS is not case sensitive.
Syntax:
SELECT The CONTAINS predicate supports complex syntax for searching:
To search for a word in a field:
SELECT title_id, title, notes FROM titles To locate a phrase in a field:
SELECT title_id, titles, notes FROM titles To locate a phrase in all enabled fields:
SELECT title_id, titles, notes FROM titles Using AND, OR and NOT
SELECT title, notes FROM titles
SELECT titles, notes FROM titles SELECT titles, notes FROM titles SELECT titles, notes FROM titles
Using wildcards
SELECT titles, notes FROM titles
SELECT titles, notes FROM titles
Search for words or phrases indicating the importance of the words:
This search allows you to indicate the weight that each of the searched words or phrases will have on the search result, the weight ranges between the lowest value 0.0 and the highest value 1.0.
SELECT Customer, Name, Address FROM Customer (All those records will be found that contain the string street followed by any value in the address field, ordering those of “Calle Serrano” first, then those of “Calle Velázquez” and then the rest.
Search for nearby words:
We can search for two words and indicate that they are close to each other. The order of the words does not alter the search result.
SELECT title, notes FROM books
SELECT title, notes FROM books
Three words can be indicated, in such a way that the second and the first must be close as well as the second and the third.
SELECT title, notes FROM books Phrase searches:
… WHERE CONTAINS(Description, ” sauces ~ “mix*” “)
… WHERE CONTAINS(Description, ” “meat*” ~ “patty sauce*” “)
When using this predicate, all the words of the phrases are analyzed and it returns us as a result, those records that contain the complete phrase or some fragment of it. The syntax is the same as the CONTAINS predicate.
… FREETEXT(description, ‘ “In a place of the stain whose name I don’t want to remember” ‘) This predicate has the same function and syntax as CONTAINS, except that in this case it returns a table with two columns, the first call contains the value of the primary key of the table that we are looking for, the second RANK call returns a value indicating the percentage search hit for each record.
SELECT Questions.Question, Questions.Answer, Result.RANK SELECT Questions.Question, Questions.Answer, Result.RANK
The FREETEXTTABLE predicate
It is the equivalent of CONTAINSTABLE but performing FREETEXT searches.
WHERE CONTAINS(
WHERE CONTAINS(notes,’business’)
WHERE CONTAINS(notes,’ “common business applications” ‘)
WHERE CONTAINS(*, ‘ “common business applications” ‘)
WHERE CONTAINS(notes, ‘ “favorite recipes” OR “gourmet recipes” ‘)
WHERE CONTAINS(notes, ‘ cooking AND NOT (“computer*”) ‘)
WHERE CONTAINS(notes, ‘ beer AND ales ‘)
WHERE CONTAINS(*, ‘(“ice skating” OR hockey) AND NOT olympics’)
WHERE CONTAINS(notes,’ “ice*” ‘)
WHERE CONTAINS(notes, ‘ “light bread*” ‘)
WHERE CONTAINS (Address, ‘ISABOUT ( “Street*”, Velazquez WEIGHT(0.5), Serrano(0.9)’)
WHERE CONTAINS (notes, “user NEAR computer”)
WHERE CONTAINS (notes, “user ~ computer”)
WHERE CONTAINS (notes, “user ~ beginner ~ computer”)
The FREETEXT predicate
The CONTAINSTABLE predicate
FROM Questions, CONTAINSTABLE(Response, ” SQL Server”) AS Result
WHERE Questions.IdQuestion = Result.
ORDER BY Result.RANK Desc
FROM Questions INNER JOIN CONTAINSTABLE (Response, “SQL Server”) AS Result
ON Questions.IdQuestion = Results.KEY