Skip to main content

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

OptionTypeDefaultDescription
SYNONYMSstringrequiredInline 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.

InputSynonyms mapTokens
carcar, automobile, auto{auto,automobile,car}
automobilecar, automobile, auto{auto,automobile,car}
laptoplaptop => notebook{notebook}
keyboard(no matching rule){keyboard}

The map below combines a bidirectional class with a one-way rule:

Query
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:

Query
SELECT ts_lexize('solr_syn', 'car');
Result
 ts_lexize----------------------- {auto,automobile,car}
Query
SELECT ts_lexize('solr_syn', 'automobile');
Result
 ts_lexize----------------------- {auto,automobile,car}

The left side of a one-way rule rewrites to its right side:

Query
SELECT ts_lexize('solr_syn', 'laptop');
Result
 ts_lexize------------ {notebook}

A term that matches no rule passes through unchanged:

Query
SELECT ts_lexize('solr_syn', 'keyboard');
Result
 ts_lexize------------ {keyboard}

See also

This page contains: