Synonym analyser not working in general term search across fields

I have added synonym mapping as per the given format. It is added on the settings. The mapping has "universe" mapped to "cosmos".

The below URI search gives me a match
_localhost:9200/indexname/search?q=data.summary:cosmos

But this one does not work.
_localhost:9200/indexname/search?q=cosmos

Why ?
I want to search across all the fields and still make a match. That is not working. I have given settings and JSON below.

Settings JSON

{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2,
    "index": {
      "analysis": {
        "analyzer": {
          "synonym_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "lowercase",
              "synonym"
            ]
          }
        },
        "filter": {
          "synonym": {
            "type": "synonym",
            "synonyms_path": "analysis/synonym.txt",
            "tokenizer": "whitespace"
          }
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "data": {
          "properties": {
            "summary": {
              "type": "text",
              "store": true,
              "analyzer": "synonym_analyzer"
            }
            }
          }
        }
      }
    }
  }
}

JSON Document
{
"guid": "123",
"data": {
"summary": "All Space-X rockets going to the universe",
"status": "Active"
}
}

Have you tried adding &explain=true to see which fields are searched? And the terms being expanded at query time if any?

I don’t know what happened when you don’t field the search, but probably you are searching some field that does not have the synonym analyzer enabled. Or there is a multi field search that falls back to a different query analyzer.

I also wouldn’t use the query URL for anything beyond simple poking around. For production, it’s best to use the JSON query DSL which would let you be even more explicit to how you want to search.

Doug

1 Like

Do you think this is because of the difference between Match Query & Term Query ?

Also, when I specify field name does it internally change from a term query to a match query ?

Currently I am using the Java Highlevel Client and using QueryBuilders to generate a Term Query.

BTW, the "explain=true " is not helpful when there is no match.

In that case you can also use the "_explain" API, pointing it to a document you think should match.

OK. I found out the solution. The problem was with the search parameters. It will work if we specify an additional 'analyzer' parameter.

Thisquery DSL documentation listed all the parameters.

So the query string looks like below.
localhost:9200/indexname/ search?q=cosmos&analyzer=synonym_analyzer

1 Like

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