Full-Text Search
This page describes the SQL surface of PostgreSQL-style full-text search: the tsvector and tsquery types, the @@ operator, the parsing, ranking and highlighting functions, the pg_trgm and unaccent functions, and how a GIN index accelerates @@. The posting-list structure behind the index is described in Full-text indexing and in GIN: full-text postings.
All of it is implemented in src/query/fts.rs without an external text-search library. The Tantivy crate (tantivy = "0.22" in Cargo.toml) is a dependency used by src/index/fulltext.rs, which SQL does not reach. Not wired: src/index/fulltext.rs from SQL. (source: src/query/fts.rs; Cargo.toml; src/index/fulltext.rs)
Types and their text forms
tsvector and tsquery values are stored and passed as text in PostgreSQL's output form; they report OIDs 3614 and 3615.
| Type | Text form | Example |
|---|---|---|
tsvector |
Sorted, de-duplicated lexemes, each quoted, with optional positions and weights | 'cat':3 'fat':2A 'rat':5 |
tsquery |
Quoted operands joined by &, \|, !, <-> or <n>, with :* for prefix and weight letters, parenthesised by precedence |
'fat' & ( 'rat' \| 'cat' ) |
A weight of D is the default and is not printed. Positions are stored as 16-bit integers. (source: src/query/fts.rs, format_tsvector, parse_tsvector, format_tsquery, Lexeme; src/query/types.rs, PgTypeOid::TsVector, PgTypeOid::TsQuery)
A cast 'a fat cat'::tsvector or 'fat & rat'::tsquery canonicalises the literal without normalising words (no stemming, no stop-word removal), as PostgreSQL does. (source: src/query/fts.rs, cast_text, parse_tsquery_string)
The module's header comment says phraseto_tsquery is "treated as AND here"; the code builds a phrase query with distances, so the comment is out of date. (source: src/query/fts.rs, module comment, phraseto_tsquery, phrase_of)
Text search configurations
Only two behaviours exist, selected by the configuration name:
| Configuration | Behaviour |
|---|---|
english, pg_catalog.english
|
Lower-case, drop English stop words (PostgreSQL's list), Porter-stem each word |
any other name (simple, german, myconfig, …) |
Lower-case only; no stop words, no stemming |
A function called without a configuration, or with a configuration argument that is not a string, uses english. default_text_search_config is not consulted. (source: src/query/fts.rs, is_english, normalize_word, english_stop_words, porter_stem; src/query/engine.rs, to_tsvector arm)
CREATE TEXT SEARCH CONFIGURATION ... (COPY = english) is accepted and does nothing; a configuration created that way behaves as the non-English row above. (source: src/query/engine.rs, test test_fts_create_text_search_config_noop)
The tokenizer splits on any character that is not alphanumeric or an apostrophe, strips leading and trailing apostrophes, and lower-cases ASCII letters only. It does not recognise the special token types PostgreSQL's parser has (URLs, e-mail addresses, hyphenated words, numbers with decimals, file paths), so those split into plain words. Non-ASCII upper-case letters are not lower-cased. (source: src/query/fts.rs, tokenize)
The @@ operator
a @@ b evaluates to true when the tsvector side matches the tsquery side:
- The tsquery side is chosen by the expression type (a
::tsquerycast or a tsquery function), so either order works. - The tsvector side is used as is when it is typed
tsvectoror its text already looks like a tsvector. Otherwise it is plain text and is converted withto_tsvector('english', ...). - The query text is parsed as a stored tsquery: operands are taken literally, not stemmed.
- NULL on either side gives NULL.
(source: src/query/engine.rs, ts_match_values, BinaryOp::TsMatch arm; src/query/fts.rs, looks_like_tsvector, tsvector_matches, parse_tsquery_string)
Matching rules: a lexeme operand matches exactly, or as a prefix with :*; weight letters restrict which lexeme weights match; ! negates; <-> and <n> require the right operand at exactly n positions after the left. (source: src/query/fts.rs, match_node, lexeme_hits, phrase_positions)
Because the query side is not normalised, body @@ 'running' with an unstemmed word does not match the stemmed lexeme run. Write body @@ to_tsquery('english', 'running'). In PostgreSQL, text @@ text applies plainto_tsquery to the right side. (source: src/query/engine.rs, ts_match_values)
Functions
| Function | Result | Notes |
|---|---|---|
to_tsvector([cfg,] text) |
tsvector | NULL text gives NULL |
to_tsquery([cfg,] text) |
tsquery | Operators &, \|, !, <->, <n>, parentheses, :* and weight suffixes; operands normalised by cfg
|
plainto_tsquery([cfg,] text) |
tsquery | Words joined with &
|
phraseto_tsquery([cfg,] text) |
tsquery | Words joined with <->, using real position gaps so removed stop words keep their slot |
websearch_to_tsquery([cfg,] text) |
tsquery |
"quoted phrase", -word (NOT), or between groups; other words joined with &
|
setweight(tsvector, 'A'\|'B'\|'C'\|'D') |
tsvector | |
tsvector \|\| tsvector |
tsvector | Positions of the right side shifted past the left side |
strip(tsvector) |
tsvector | Positions removed |
tsvector_to_array(tsvector) |
text[] | Lexemes |
ts_rank(tsvector, tsquery) |
float4 | PostgreSQL's ts_rank formula with normalisation 0 and weights 0.1, 0.2, 0.4, 1.0 for D, C, B, A; only the two-argument form is supported (a leading weights array or a trailing normalisation argument is not interpreted) |
ts_rank_cd(tsvector, tsquery) |
float4 | Same computation as ts_rank; cover density is not implemented |
ts_headline([cfg,] text, tsquery [, options]) |
text | Options StartSel, StopSel, MaxWords, MinWords; other options ignored |
numnode(tsquery) |
integer | Counts quoted operands (half the number of ' characters), not operator nodes; differs from PostgreSQL for any query with an operator |
querytree(tsquery) |
text | Returns the query text unchanged |
ts_debug, ts_lexize, ts_parse, ts_token_type
|
NULL | Accepted, return NULL |
(source: src/query/engine.rs, arms to_tsvector, to_tsquery, plainto_tsquery, phraseto_tsquery, websearch_to_tsquery, setweight, strip, tsvector_to_array, ts_rank | ts_rank_cd, ts_headline, numnode, querytree, ts_debug | ts_lexize | ts_parse | ts_token_type; src/query/fts.rs, to_tsvector, parse_tsquery, plainto_tsquery, phraseto_tsquery, websearch_to_tsquery, setweight, concat_tsvectors, ts_rank, rank_weight, ts_headline)
ts_rank returns 0 (not NULL) when either argument is NULL. (source: src/query/engine.rs, ts_rank | ts_rank_cd arm)
Tested by unit tests test_porter_stem_basic, test_to_tsvector_english, test_tsvector_match_basic, test_tsvector_no_match, test_ts_rank_matches_postgres_formula, test_ts_rank_positive, test_setweight, test_concat_tsvectors and test_ts_headline in fts.rs; engine tests test_fts_to_tsvector_returns_non_empty, test_fts_tsmatch_true, test_fts_tsmatch_false, test_fts_ts_rank_positive, test_fts_where_clause_filter, test_fts_plainto_tsquery, test_fts_generated_tsvector_column and test_fts_match_common_shapes; and feature-matrix category fts (tsvector-match, plainto-tsquery, phraseto-tsquery, ts-rank, websearch-tsquery). (source: src/query/fts.rs, tests; src/query/engine.rs, tests; test-scripts/feature-matrix/features.py)
The guide, citing the repository README's known gaps, states that to_tsvector(...) @@ to_tsquery(...) matches PostgreSQL on 8 full-text probes; see How it works. MySQL MATCH (...) AGAINST (...) is mapped onto the same tsvector search, as the guide also describes. (source: README.md, Known gaps; docs/status/2026-11-24-app-install-gaps.md)
Trigram similarity and unaccent
| Function or operator | Result | Notes |
|---|---|---|
similarity(a, b) |
float | Shared trigrams over the union of trigrams |
word_similarity(a, b) |
float | Best match of a against a word span of b
|
strict_word_similarity(a, b) |
float | Word-boundary variant |
show_trgm(s) |
text[] | The trigram set |
a % b (both strings) |
bool | similarity(a, b) >= 0.3 |
similarity_threshold() |
float | Always 0.3 |
unaccent(s) |
text | Strips diacritics from Latin letters with a fixed mapping table |
The threshold is fixed: pg_trgm.similarity_threshold cannot be changed with SET, and the <%, %>, <<% and <-> trigram operators are not implemented (<-> is pgvector distance). There is no trigram index; % and similarity are evaluated row by row. (source: src/query/fts.rs, trigrams, similarity, word_similarity, strict_word_similarity, unaccent, strip_diacritic; src/query/engine.rs, similarity_threshold arm, BinaryOp::TrgmSimilarity arm)
Tested by unit tests test_similarity_close, test_similarity_identical, test_trigrams and test_unaccent_basic; engine tests test_fts_similarity, test_fts_trgm_percent_operator and test_fts_unaccent; feature-matrix scenarios fts/similarity, fts/trgm-pct-op and fts/unaccent. (source: src/query/fts.rs, tests; src/query/engine.rs, tests; test-scripts/feature-matrix/features.py)
GIN indexes
A GIN index makes @@ use posting lists instead of scanning the table. Two index forms are recognised:
CREATE INDEX docs_tsv_idx ON docs USING gin (tsv); -- a tsvector column
CREATE INDEX docs_body_fts ON docs USING gin (to_tsvector('english', body)); -- an expression
(source: src/query/engine.rs, tests fts_index_matches_brute_force_exactly, fts_index_rebuilt_after_restart)
try_fts_candidates is consulted during a table scan when the WHERE clause has an @@ conjunct. It uses the index only when all of these hold:
- The statement is not inside an explicit transaction.
- The tsquery side does not reference a column (a literal, a parameter or a constant expression).
- The tsvector side is the indexed column, or
to_tsvector(cfg, col)with the same configuration string as the index expression (to_tsvector(col)counts asenglish). - The in-memory index entry is not marked dirty.
When it applies, the query tree is resolved against the posting lists (intersection for &, union for |; a negation places no restriction), the candidate rows are fetched by key, and the full WHERE predicate is applied again to them, so the result equals a full scan. Otherwise the scan proceeds without the index. (source: src/query/engine.rs, try_fts_candidates, fts_key_of_expr, fts_index_sources, scan path calling try_fts_candidates)
(source: src/query/engine.rs, try_fts_candidates)
The index is maintained on INSERT, UPDATE and DELETE and rebuilt after a restart. Tested by fts_index_matches_brute_force_exactly, fts_index_maintained_on_insert_update_delete, fts_accelerated_path_is_actually_used, fts_index_rebuilt_after_restart and test_fts_gin_index_accepted. (source: src/query/engine.rs, tests)
fts_index_speedup_report is a timing test that prints a comparison; its numbers are not reproduced here. (source: src/query/engine.rs, test fts_index_speedup_report)
A GIN index on a jsonb or array column serves @>, ?, ?| and ?& through try_gin_candidates instead; see GIN indexes. (source: src/query/engine.rs, try_gin_candidates)