Synonym handling helps search engines find results even if words are different but mean the same thing.
Synonym handling in Elasticsearch
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms": [
"car, automobile",
"quick, fast"
]
}
},
"analyzer": {
"my_synonym_analyzer": {
"tokenizer": "standard",
"filter": ["lowercase", "my_synonym_filter"]
}
}
}
},
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "my_synonym_analyzer"
}
}
}
}The synonym filter lists words that mean the same.
The analyzer uses this filter to treat synonyms as equal during search.
PUT /books
{
"settings": {
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms": ["happy, joyful, glad"]
}
},
"analyzer": {
"synonym_analyzer": {
"tokenizer": "standard",
"filter": ["lowercase", "synonym_filter"]
}
}
}
},
"mappings": {
"properties": {
"description": {
"type": "text",
"analyzer": "synonym_analyzer"
}
}
}
}synonyms.txt for a products index.PUT /products
{
"settings": {
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms_path": "analysis/synonyms.txt"
}
},
"analyzer": {
"synonym_analyzer": {
"tokenizer": "standard",
"filter": ["lowercase", "synonym_filter"]
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "synonym_analyzer"
}
}
}
}This program creates an index where 'car', 'automobile', and 'auto' are synonyms. It adds a document with 'automobile' and searches using 'car'. The search will find the document because of synonym handling.
PUT /vehicles
{
"settings": {
"analysis": {
"filter": {
"vehicle_synonyms": {
"type": "synonym",
"synonyms": [
"car, automobile, auto",
"bike, bicycle"
]
}
},
"analyzer": {
"vehicle_analyzer": {
"tokenizer": "standard",
"filter": ["lowercase", "vehicle_synonyms"]
}
}
}
},
"mappings": {
"properties": {
"description": {
"type": "text",
"analyzer": "vehicle_analyzer"
}
}
}
}
POST /vehicles/_doc/1
{
"description": "I love my automobile"
}
GET /vehicles/_search
{
"query": {
"match": {
"description": "car"
}
}
}Synonyms improve search but can make indexing slower.
Keep synonym lists simple to avoid confusing results.
Synonym filters work best when applied at index time or search time, but not both.
Synonym handling helps find results with different words that mean the same.
Define synonyms in filters and use them in analyzers.
Use synonyms to improve search experience without changing data.
Practice
Solution
Step 1: Understand synonym handling concept
Synonym handling allows Elasticsearch to match words that mean the same, improving search results.Step 2: Identify the main goal
The goal is to find results with different words but same meaning, not speed or encryption.Final Answer:
To find search results that include different words with the same meaning -> Option AQuick Check:
Synonym handling = find similar meaning words [OK]
- Thinking synonyms speed up indexing
- Confusing synonyms with encryption
- Assuming synonyms translate languages automatically
Solution
Step 1: Identify correct filter type for synonyms
The filter type for synonyms must be "synonym" to handle synonym lists.Step 2: Check the filter definition syntax
"filter": { "synonym_filter": { "type": "synonym", "synonyms": ["quick,fast"] } } correctly uses "type": "synonym" and defines synonyms as a list.Final Answer:
"filter": { "synonym_filter": { "type": "synonym", "synonyms": ["quick,fast"] } } -> Option CQuick Check:
Synonym filter type = "synonym" [OK]
- Using wrong filter types like stop or stemmer
- Not defining synonyms as a list
- Misspelling "synonym" type
"fast car"?
{
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms": ["fast,quick"]
}
},
"analyzer": {
"synonym_analyzer": {
"tokenizer": "standard",
"filter": ["lowercase", "synonym_filter"]
}
}
}
}Solution
Step 1: Analyze the text with the synonym filter
The synonym filter maps "fast" to "quick", so both tokens appear for "fast".Step 2: List all tokens after analysis
Tokens are "fast", "quick" (synonym), and "car" (unchanged).Final Answer:
["fast", "quick", "car"] -> Option BQuick Check:
Synonym adds "quick" token for "fast" [OK]
- Ignoring synonym addition and listing only original tokens
- Replacing original token instead of adding synonym
- Duplicating tokens unnecessarily
Solution
Step 1: Understand how synonym filters affect search
Synonyms must be applied in the analyzer used during search to affect results.Step 2: Identify common setup mistake
Often synonyms are applied only at index time, missing search analyzer usage.Final Answer:
You used the synonym filter in the index but not in the search analyzer -> Option AQuick Check:
Synonyms must be in search analyzer too [OK]
- Adding synonym filter only at index time
- Not including synonym filter in search analyzer
- Misconfiguring synonyms as string instead of list
Solution
Step 1: Define all related terms as synonyms
Include "tv", "television", and "smart tv" together to cover all user queries.Step 2: Apply synonym filter in both index and search analyzers
This ensures synonyms work during indexing and searching for best results.Final Answer:
Define synonyms as: "tv, television, smart tv" in a synonym filter and apply it in both index and search analyzers -> Option DQuick Check:
Synonyms cover all terms and apply at both stages [OK]
- Ignoring multi-word synonyms like "smart tv"
- Applying synonyms only at search time
- Using stop filter incorrectly to remove words
Ready to prove it?
Pass the test and this pattern moves unknown
