Prioritizing Original Terms over Synonyms in Elastic Search
In My Analyzer Pipeline I Have a Synonyms Function. Let's Say That I Have the Following Synonyms Beverage, Drink Now Let's Say That a User Searches for...
In my analyzer pipeline I have a synonyms function.
Let's say that I have the following synonyms
beverage, drink
Now let's say that a user searches for 'beverage', the user will get documents that contain 'beverage' or 'drink' without any preference.
The thing is that I want to give a higher score to documents that contain the original search term ('beverage' in this case) and a lower score to its synonyms ('drink' in this case).
What's the best and cheapest way for doing that?
1 Answer
You can create another subfield in the mapping without the synonyms.
In the query you match on both fields.
Index
PUT synonym_index
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"synonym": {
"tokenizer": "standard",
"filter": [
"synonym"
]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms": [
"beverage, drink"
]
}
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"search_analyzer": "synonym",
"fields": {
"no_synonyms": {
"type": "text"
}
}
}
}
}
}
Documents
POST synonym_index/_doc/1
{
"name": "beverage"
}
POST synonym_index/_doc/2
{
"name": "drink"
}
Query
GET synonym_index/_search
{
"query": {
"multi_match": {
"query": "beverage",
"type": "most_fields",
"fields": [
"name",
"name.no_synonyms"
]
}
}
}