The 30 second answer
I prefer query time with a synonym_graph filter in the search analyzer. It handles multi-word synonyms correctly, and I can change the rules without reindexing; synonym sets from the synonyms API reload analyzers automatically. Index time makes queries slightly cheaper but needs a reindex for every rule change and doesn't handle multi-word synonyms well. The cost of query time is more terms per query.
What synonyms do. A rule like trainers, sneakers makes a search for one word also find the other. The rule lives in an analyzer, so the question is which analyzer: the one used at index time, the one used at query time, or both.
What the docs say.
- The
synonym_graphfilter is "recommended for search analyzers" and "correctly handles multi-word synonyms". It's "designed for search-time use only". - The plain
synonymfilter is "required for index-time synonyms" and "not recommended if you need to use multi-word synonyms". - Synonym sets created through the synonyms API or Kibana "can only be used at search time", and changes "automatically reload the associated analyzers". A filter using a synonym set is marked
"updateable": true.
Badly written. Synonyms baked in at index time. Every change of the list (new slang, a new brand alias) needs a full reindex, and "t shirt" as a two-word synonym gets mangled:
"analyzer": { "idx": { "tokenizer": "standard",
"filter": ["lowercase", "synonym_idx"] } }Better. A normal index analyzer, and synonyms only in the search analyzer:
PUT products
{ "settings": { "analysis": {
"filter": { "shop_synonyms": { "type": "synonym_graph",
"synonyms_set": "fashion-synonyms", "updateable": true } },
"analyzer": {
"idx": { "tokenizer": "standard", "filter": ["lowercase"] },
"search": { "tokenizer": "standard", "filter": ["lowercase", "shop_synonyms"] } } } },
"mappings": { "properties": {
"name": { "type": "text", "analyzer": "idx", "search_analyzer": "search" } } } }
PUT _synonyms/fashion-synonyms
{ "synonyms_set": [
{ "id": "sneakers", "synonyms": "sneakers, trainers" },
{ "id": "tshirt", "synonyms": "t shirt, tee, tshirt" } ] }Trade-offs.
- Query-time synonyms expand the query into more terms, so each search does a bit more work. Index-time synonyms make the index bigger and freeze the rules into the data.
- Synonyms can hurt precision: "jumper" means a sweater in British English and a pinafore dress in American English. Per-market synonym sets are often needed in a multi-market shop.
- Treat synonym changes like code: test them against a judgment set before rollout.
Alternate interview scenarios
"Merchandising wants to add synonyms every day without engineering involvement. How?"
Use a search-time synonym set managed through the synonyms API behind an internal tool, with validation and a judgment-set check before publishing, since changes reload analyzers without a reindex.
"A synonym made results worse for one market only."
Use separate synonym sets per market or per language analyzer, so a rule for one language doesn't leak into another.