Implementing Full-Text Search
This lesson explains full-text catalogs and indexes, the CONTAINS and FREETEXT predicates and ranked results.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the requirements of a full-text index.
- Distinguish between CONTAINS and FREETEXT.
- Explain how CONTAINSTABLE and FREETEXTTABLE return ranked results.
Full-text catalogs and indexes
A full-text index stores the words (tokens) of one or more character, xml or varbinary columns of a table, produced by language-specific word breakers and stemmers, minus words in a stoplist. A table can have one full-text index, which needs a unique, single-column, non-nullable key index, and belongs to a full-text catalog. Population runs in the background, and CHANGE_TRACKING = AUTO keeps the index updated as rows change. Full-text search is available in SQL Server, Azure SQL Database and Azure SQL Managed Instance.
CREATE FULLTEXT CATALOG ftCatalog AS DEFAULT;
CREATE FULLTEXT INDEX ON Support.Article (Title LANGUAGE 1033, Body LANGUAGE 1033)
KEY INDEX PK_Article
WITH CHANGE_TRACKING = AUTO, STOPLIST = SYSTEM;Full-text index parts
CONTAINS and FREETEXT
Two predicates filter rows, as the following table compares.
| Predicate | Matches | Example |
|---|---|---|
| CONTAINS | Precise terms, phrases, prefixes, inflections, proximity, Boolean logic | CONTAINS(Body, '"tent pole" AND FORMSOF(INFLECTIONAL, repair)') |
| FREETEXT | The meaning of a free phrase: words, their inflections and thesaurus expansions, matched loosely | FREETEXT(Body, 'how to fix a broken tent pole') |
CONTAINS supports "hik*" prefix terms, NEAR((stove, wind), 5) proximity and AND, OR and AND NOT. FREETEXT is simpler for user-typed phrases but less precise.
CONTAINS or FREETEXT?
Ranked results with CONTAINSTABLE and FREETEXTTABLE
CONTAINSTABLE and FREETEXTTABLE are table-valued functions that return the matching rows' [KEY] and a RANK from 0 to 1,000, which are joined back to the table and ordered by RANK. An optional top_n_by_rank argument limits the number of rows. These ranked lists are the keyword side of hybrid search.
SELECT TOP (20) a.ArticleID, a.Title, ft.[RANK]
FROM FREETEXTTABLE(Support.Article, (Title, Body), N'stove will not stay lit in wind') AS ft
JOIN Support.Article AS a ON a.ArticleID = ft.[KEY]
ORDER BY ft.[RANK] DESC;Ranked keyword results
A search page must show the 20 best keyword matches for a user's free-text query with a relevance score, and the results will later be fused with vector search. Which construct should the developer use?
Use FREETEXTTABLE (or CONTAINSTABLE for precise syntax) joined to the table on [KEY], ordered by RANK descending with TOP (20). The predicates FREETEXT and CONTAINS filter rows but return no rank, and LIKE '%word%' cannot use the full-text index and has no relevance ranking.
Which columns does CONTAINSTABLE return?
Show answer
[KEY], the full-text key value, and RANK, from 0 to 1,000.
Exam Tips
Remember for the exam
- A full-text index needs a unique, single-column, non-nullable key index; one per table.
- Precise syntax: CONTAINS; natural phrases: FREETEXT.
- Ranked results: CONTAINSTABLE or FREETEXTTABLE with KEY and RANK.
- Keep the index current: CHANGE_TRACKING = AUTO.