Multi words synonym query issues

Hi guys,

I am having a challenge with multi words synonym related queries. E.g I have the following format of synonym:

Spanish onion, red onion

I have a document which contains words “onion red”. When I use multi match “cross_fields” searching with query “red onion”, or “Spanish onion”, it doesn’t return the above product. It looks like adding the synonym filter caused the problem. I tried to specify “slop”, however it didn’t work at all. Does anyone have any ideas?

You don't provide a lot of details with regards to how you have implemented these synonyms, but I'm assuming you're using the synonym_graph token filter in a search analyzer?

When you define multi-word synonyms using the synonym_graph token filter, those multi-words are then treated as phrases, even in a regular match query (or multi_match query). So, by defining spanish onion and red onion as synonyms, you will now no longer search for red OR onion, but for the phrase "red onion" (and its synonyms) instead. This is why you can't find your document.

As a way around this you could use a bool query that searches both with and without synonyms, by overriding the analyzer to be the standard analyzer in one of the queries. That would look something like this:

GET my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "red onion",
            "fields": [
              "my_field"
            ],
            "type": "cross_fields"
          }
        },
        {
          "multi_match": {
            "query": "red onion",
            "fields": [
              "my_field"
            ],
            "type": "cross_fields",
            "analyzer": "standard"
          }
        }
      ]
    }
  }
}

Hi Abodn,

Thanks for your reply and suggestions.

It was resolved by setting the parameter “ auto_generate_synonyms_phrase_query“ to false.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.