# 8.11. Text Search Types

8.11.1.tsvector

8.11.2.tsquery

PostgreSQL provides two data types that are designed to support full text search, which is the activity of searching through a collection of natural-languagedocumentsto locate those that best match aquery. Thetsvectortype represents a document in a form optimized for text search; thetsquerytype similarly represents a text query.Chapter 12provides a detailed explanation of this facility, andSection 9.13summarizes the related functions and operators.

# 8.11.1.tsvector

Atsvectorvalue is a sorted list of distinctlexemes, which are words that have beennormalizedto merge different variants of the same word (seeChapter 12for details). Sorting and duplicate-elimination are done automatically during input, as shown in this example:

SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector;
                      tsvector
### 8.11.2. `tsquery`

[]()

 A `tsquery` value stores lexemes that are to be searched for, and can combine them using the Boolean operators `&` (AND), `|` (OR), and `!` (NOT), as well as the phrase search operator `<->` (FOLLOWED BY). There is also a variant `<*`N`*>` of the FOLLOWED BY operator, where *`N`* is an integer constant that specifies the distance between the two lexemes being searched for. `<->` is equivalent to `<1>`.

 Parentheses can be used to enforce grouping of these operators. In the absence of parentheses, `!` (NOT) binds most tightly, `<->` (FOLLOWED BY) next most tightly, then `&` (AND), with `|` (OR) binding the least tightly.

 Here are some examples:

SELECT 'fat & rat'::tsquery; tsquery