AI Hints
Home›Elasticsearch Tutorial›Stemming, Synonyms and Custom Analyzers
Elasticsearch · Lesson 56 of 120

Synonyms

Coat means jacket.

⏱ 2 min read▶ Video at 52:41🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Customers say coat, but Trailhead sells jackets. They say torch, but the shop calls them headlamps. Synonyms teach search that different words mean the same thing.

  • coat = jacket
  • torch = headlamp
  • Synonyms

A synonym analyzer

We define a synonym graph filter with two rules. The search analyzer uses it, so it applies to what customers type. Then we search for coat. Run it. The search for coat finds the rain jacket.

Dev Tools Consoletext
PUT products
{ "settings": { "analysis": {
    "filter": { "shop_syn": { "type": "synonym_graph",
      "synonyms": ["coat, jacket", "torch, headlamp"] } },
    "analyzer": { "syn": { "tokenizer": "standard",
      "filter": ["lowercase", "shop_syn"] } } } },
  "mappings": { "properties": { "name": {
    "type": "text", "search_analyzer": "syn" } } } }

PUT products/_doc/1?refresh=true&filter_path=result
{ "name": "Storm Rain Jacket" }

GET products/_search?filter_path=hits.hits._source
{ "query": { "match": { "name": "coat" } } }
Output
# PUT products 200 OK
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "products"
}

# PUT products/_doc/1 201 Created
{"result": "created"}

# GET products/_search 200 OK
{"hits": {"hits": [{"_source": {"name": "Storm Rain Jacket"}}]}}

Synonym tips

Use synonym graph at search time, so you can change synonyms without reindexing. Commas mean the words are equal. An arrow means one way only. And keep big lists in a synonyms set, managed with its own API.

keyvalue
synonym_graphat search time
a, bequal words
a => bone way
Big listssynonyms sets API

Summary

A synonym filter makes different words match. Apply it at search time, so you can update it freely. Next, building your own analyzer.

Key takeaways
  • synonym_graph
  • search_analyzer
  • coat finds jacket
Next up: Custom Analyzers — your own pipeline