Sub topic 14
Dictionaries: stop words, stemming, synonyms and thesaurus
Assume you know basic SQL and can run PostgreSQL queries. This chapter focuses on how a text search configuration turns tokens into searchable lexemes.
How dictionaries process text
A text search configuration connects three parts:
- A parser splits text into tokens such as words, numbers, and email addresses.
- A dictionary decides what each token means.
- The resulting lexemes form a
tsvectorortsquery.
A dictionary can discard a token, normalize it, or expand it into several lexemes.
Stop words
Stop words are common words that usually add little search value, such as the, a, and is. The built-in english configuration removes many of them:
SELECT to_tsvector('english', 'The fox is running through the park');The result contains important terms but omits stop words. Removing them reduces index size and prevents common words from matching almost every document.
Stemming
Stemming reduces related word forms to a shared lexeme. For example, running, runs, and possibly ran may map to forms based on the English stemmer. This lets a search for one form match related forms.
The result depends on the language. The english configuration is appropriate for English prose, but it may produce poor results for names, identifiers, or another language.
| Dictionary type | Typical behavior | Example use |
|---|---|---|
| Stop-word list | Discards tokens | `the`, `and` |
| Stemmer | Normalizes word forms | `running` to a stem |
| Synonym | Maps one term to another | `postgres` to `postgresql` |
| Thesaurus | Maps phrases or concepts | `full text search` to a controlled term |
Synonyms and thesaurus rules
A synonym dictionary maps alternative spellings or names to a common term. A thesaurus dictionary can recognize multiword phrases and expand them. These dictionaries use rule files stored where the PostgreSQL server can read them. The file name is configured without its file extension.
For a synonym dictionary, a rule file might contain:
postgresql postgres
postgres postgresYou then create and place the dictionary before the stemmer in a configuration mapping:
CREATE TEXT SEARCH DICTIONARY app_synonyms (
TEMPLATE = synonym,
SYNONYMS = app_synonyms
);
ALTER TEXT SEARCH CONFIGURATION app_english
ALTER MAPPING FOR word
WITH app_synonyms, english_stem;The synonym dictionary runs first. If it does not recognize a token, english_stem handles it. After changing dictionary files, reload the configuration as required by your PostgreSQL version and deployment process.
Inspecting dictionary behavior
Use ts_debug to see the parser token, dictionaries tried, and lexemes produced:
SELECT alias, token, dictionaries, lexemes
FROM ts_debug('english', 'The foxes were running');This is useful when a search fails because a word became a stop word, was stemmed unexpectedly, or never matched a synonym rule.
Exercise
Why should a synonym dictionary usually appear before a stemmer?
Show answer
It must recognize the original spelling before the stemmer changes the token into a normalized form.