> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# TSQUERY

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

The `TSQUERY` <InternalLink path="data-types">data type</InternalLink> stores a list of lexemes separated by operators. `TSQUERY` values are used in <InternalLink path="full-text-search">full-text search</InternalLink>.

## Syntax

A `TSQUERY` comprises individual lexemes and operators in the form: `'These' & 'lexemes' & 'are' & 'not' & 'normalized' & 'lexemes.'`.

The operators in a `TSQUERY` are used to <InternalLink path="full-text-search#match-queries-to-documents">match a `TSQUERY` to a `TSVECTOR`</InternalLink>. Valid `TSQUERY` operators are:

* `&` (AND). Given `'one' & 'two'`, both `one` and `two` must be present in the matching `TSVECTOR`.
* `|` (OR). Given `'one' | 'two'`, either `one` or `two` must be present in the matching `TSVECTOR`.
* `!` (NOT). Given `'one' &! 'two'`, `one` must be present and `two` must **not** be present in the matching `TSVECTOR`.
* `<->` (FOLLOWED BY). Given `'one' <-> 'two'`, `one` must be followed by `two` in the matching `TSVECTOR`.
  * `<->` is equivalent to `<1>`. You can specify an integer `<n` to indicate that lexemes must be separated by `n-1` other lexemes. Given `'one' <4> 'two'`, `one` must be followed by three lexemes and then followed by `two` in the matching `TSVECTOR`.

You can optionally add the following to each lexeme:

* One or more weight letters (`A`, `B`, `C`, or `D`):

  `'These' & 'lexemes':B & 'are' & 'not' & 'normalized':A & 'lexemes':B`

  If not specified, a lexeme's weight defaults to `D`. It is only necessary to specify weights in a `TSQUERY` if they are also <InternalLink path="tsvector#syntax">specified in a `TSVECTOR`</InternalLink> to be used in a comparison. The lexemes in a `TSQUERY` and `TSVECTOR` will only match if they have matching weights. For more information about weights, see the [PostgreSQL documentation](https://www.postgresql.org/docs/15/datatype-textsearch#DATATYPE-TSQUERY).

To be usable in <InternalLink path="full-text-search">full-text search</InternalLink>, the lexemes **must be normalized**. You can do this by using the `to_tsquery()`, `plainto_tsquery()`, or `phraseto_tsquery()` <InternalLink path="functions-and-operators#full-text-search-functions">built-in functions</InternalLink> to convert a string input to `TSQUERY`:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SELECT to_tsquery('These & lexemes & are & not & normalized & lexemes.');
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
           to_tsquery
--------------------------------
  'lexem' & 'normal' & 'lexem'
```

Normalization removes the following from the input:

* Derivatives of words, which are reduced using a [stemming](https://wikipedia.org/wiki/Stemming) algorithm.
* *Stop words*. These are words that are considered not useful for indexing and searching, based on the <InternalLink path="full-text-search#text-search-configuration">text search configuration</InternalLink>. In the preceding example, "These", "are", and "not" are identified as stop words.
* Punctuation and capitalization.

## Examples

For usage examples, see <InternalLink path="full-text-search">Full-Text Search</InternalLink>.

## See also

* <InternalLink path="full-text-search">Full-Text Search</InternalLink>
* <InternalLink path="tsvector">`TSVECTOR`</InternalLink>
* <InternalLink path="data-types">Data Types</InternalLink>
