solr_synonyms
The solr_synonyms template expands tokens through a synonyms map written in Apache Solr synonyms-file format, supplied inline via the required SYNONYMS option. It rewrites each input term to the set of terms it is equivalent to, so a search for one word also finds documents written with any of its synonyms.
Each line of the map is a rule. A comma-separated list of terms forms a bidirectional equivalence class — any term in the list expands to all of them. The arrow form lhs => rhs defines a one-way mapping — the left side rewrites to the right and never the reverse. Input that matches no rule passes through unchanged.
Use it — usually inside a pipeline after a tokenizer — to broaden recall without bloating the index, since the expansion happens at analysis time on both the indexed text and the query.
Options
| Option | Type | Default | Description |
|---|---|---|---|
SYNONYMS | string | required | Inline Solr-format synonyms: one rule per line; comma-separated terms are bidirectional, lhs => rhs maps left to right |
Tokenization
A bidirectional class such as car, automobile, auto makes the three terms interchangeable: any one of them expands to all three (returned in sorted order), so a query for auto matches text that said car. A one-way rule such as laptop => notebook rewrites only in the stated direction — laptop becomes notebook, but notebook is left alone. A term that matches no rule is emitted as-is.
| Input | Synonyms map | Tokens |
|---|---|---|
car | car, automobile, auto | {auto,automobile,car} |
automobile | car, automobile, auto | {auto,automobile,car} |
laptop | laptop => notebook | {notebook} |
keyboard | (no matching rule) | {keyboard} |
The map below combines a bidirectional class with a one-way rule:
CREATE TEXT SEARCH DICTIONARY solr_syn ( template = 'solr_synonyms', -- comma-separated terms are bidirectional; "=>" maps left to right synonyms = 'car, automobile, autolaptop => notebook');Any member of the class expands to the whole class:
SELECT ts_lexize('solr_syn', 'car'); ts_lexize----------------------- {auto,automobile,car}SELECT ts_lexize('solr_syn', 'automobile'); ts_lexize----------------------- {auto,automobile,car}The left side of a one-way rule rewrites to its right side:
SELECT ts_lexize('solr_syn', 'laptop'); ts_lexize------------ {notebook}A term that matches no rule passes through unchanged:
SELECT ts_lexize('solr_syn', 'keyboard'); ts_lexize------------ {keyboard}See also
wordnet_synonyms— WordNet-format synonymspipeline— chain a tokenizer ahead of the synonym filter- CREATE TEXT SEARCH DICTIONARY