Skip to main content

CREATE TEXT SEARCH DICTIONARY

The CREATE TEXT SEARCH DICTIONARY statement defines a text search dictionary — the analyzer that turns raw text into the tokens stored in an inverted index. The dictionary controls every stage of that transformation: how text is split into tokens, how each token is normalized (case folding, accent folding, stemming) and which extra information (term positions, frequencies) is recorded for searching and ranking. The same dictionary is applied both when a column is indexed and when a full-text query runs against that column, so the data and the query are always analyzed the same way.

Every dictionary is built from a single template. A template implements one analysis strategy — splitting text on word boundaries, cutting on a delimiter, emitting character n-grams, filtering stop words and so on — and exposes its own set of options. Templates can also be composed: pipeline chains several analyzers end to end, minhash wraps another analyzer to emit similarity signatures and copy_from derives a variant of an existing dictionary.

Examples

Create a dictionary that lower-cases its input, applies English stemming and stores the term frequencies and positions needed for relevance ranking and phrase search, then attach it to two columns with an inverted index:

Query
CREATE TEXT SEARCH DICTIONARY english_dict (    template = 'text',    locale = 'en_US.UTF-8',    case = 'lower',    stemming = true,    frequency = true,    position = true);
CREATE INDEX idx_docs ON documents    USING inverted (id, title english_dict, body english_dict);

The dictionary is referenced by name in the index column list (title english_dict, body english_dict). Once the index exists, full-text queries against those columns are analyzed with the same dictionary, so a search term matches the indexed tokens even when the surface forms differ:

Query
SELECT id FROM idx_docs WHERE body @@ 'searching';
Result
 id----  1

Because english_dict stems its input, the query term searching is reduced to search and matches every row whose body contains a form of that word. To see exactly how a dictionary tokenizes a string — invaluable when tuning options — pass it to ts_lexize:

Query
SELECT ts_lexize('english_dict', 'running engines');
Result
 ts_lexize------------- {run,engin}

Templates

A dictionary must name exactly one template through the TEMPLATE option. The available templates are grouped below by what they do; follow a link for the options each one accepts.

Text processing

These templates turn human language into searchable tokens.

TemplateDescription
textTokenize into words with stemming, stopwords and accent handling
ngramGenerate character n-grams for fuzzy and substring matching
sparse_ngramGenerate sparse variable-length n-grams for substring search over code and logs
wildcardGenerate boundary-marked n-grams for wildcard and prefix matching
stemApply stemming only
normNormalize case and accents without tokenization
keywordEmit the whole input as one verbatim token
segmentationSegment text by Unicode word boundaries

Splitting & filtering

These templates carve structured text into tokens or refine an existing token stream.

TemplateDescription
delimiterSplit on a single delimiter
multi_delimiterSplit on multiple delimiters
patternMatch or split with a regular expression
path_hierarchyTokenize a path into its hierarchical prefixes
stopwordsFilter out stop words
collationProduce collation keys for sorting

Composition

These templates build a dictionary out of other dictionaries.

TemplateDescription
pipelineChain multiple analyzers in sequence
unionMerge the tokens of several analyzers run in parallel
minhashGenerate MinHash signatures with a nested analyzer
copy_fromCopy and override an existing dictionary

Synonyms

These templates expand a token into its synonyms so a search finds related wording.

TemplateDescription
solr_synonymsExpand tokens using a Solr-format synonyms map
wordnet_synonymsExpand tokens using a WordNet synonyms database

Geospatial

These templates index geometries and coordinates for geospatial search.

TemplateDescription
geojsonIndex GeoJSON geometries (points, lines, polygons)
geopointIndex latitude/longitude points

Machine learning

These templates run a pre-trained model (for example fastText) to emit tokens.

TemplateDescription
classificationML-based text classification
nearest_neighborsML-based nearest neighbor tokens

Feature flags

Independently of the template, the following flags control how much information the index records about each token. They are all off by default — enable only what your queries need, since each one increases the size of the index.

FlagDefaultDescription
FREQUENCYfalseStore term frequency (needed for relevance scoring)
POSITIONfalseStore term positions (needed for phrase queries)
NORMfalseStore the normalization factor
OFFSETfalseStore character offsets

Enable FREQUENCY when you rank results by relevance and POSITION when you run phrase or proximity queries. The dictionary in the example above sets both.

See also

  • CREATE INDEX — attach a dictionary to a column with an inverted index
  • DROP — remove a text search dictionary

Syntax