Elasticsearch exact phrase match on JSON

915 Views Asked by At

I am working on exact phrase match from a json field using the elasticsearch. I have tried mutiple syntax like multi_match, query_string & simple_query_string but they does not return results exactly as per the given phrase.

query_string syntax that I am using;

    "query":{
        "query_string":{
            "fields":[
                "json.*"
            ],
            "query":"\"legal advisor\"",
            "default_operator":"OR"
        }
    }
}

I also tried filter instead of query but filter is not given any result on json. The syntax I used for filter is;

  "query": {
    "bool": {
      "filter": {
        "match": {
          "json": "legal advisor"
        }
      }
    }
  }
}

Now the question is;

Is it possible to perform exact match operation on json using elasticsearch?

2

There are 2 best solutions below

3
On

You can try using multi-match query with type phrase

{
  "query": {
    "multi_match": {
      "query": "legal advisor",
      "fields": [
        "json.*"
      ],
      "type": "phrase"
    }
  }
}
2
On

Since you have not provided your sample docs and expected docs, I am assuming you are looking for a phrase match, Adding a working sample.

Index sample docs which will also generate the index mapping

{
    "title" : "legal advisor"
}

{
    "title" : "legal expert advisor"
}

Now if you are looking for exact phrase search of legal advisor use below query

{
    "query": {
        "match_phrase": {
            "title": "legal advisor"
        }
    }
}

Which returns only first doc

"hits": [
            {
                "_index": "64989158",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.5753642,
                "_source": {
                    "title": "legal advisor"
                }
            }
        ]