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 '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"
      ]
    }
  }
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest