1

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 Answers2

0

You can try using multi-match query with type phrase

{
  "query": {
    "multi_match": {
      "query": "legal advisor",
      "fields": [
        "json.*"
      ],
      "type": "phrase"
    }
  }
}
ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • It's working for basic case but the point is it would not handle the AND/OR/Proximity queries that query_string was handling like; (increase OR decrease OR increasing OR decreasing OR raise) "leverage ratio"~5 – Muhammad Muzammil Nov 26 '20 at 12:46
  • @MuhammadMuzammil sorry I didn't get you. Are you trying to say that with `query_string` fuzziness can be applied ? – ESCoder Nov 26 '20 at 13:38
  • proximity, AND/OR operations & exact phrase match.... all are required in one query. – Muhammad Muzammil Nov 27 '20 at 05:47
0

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"
                }
            }
        ]
Amit
  • 30,756
  • 6
  • 57
  • 88