Match_phrase is elastic search not working as expected

2.6k Views Asked by At

In my elastic search I have documents which contains a "fieldname" with values "abc" and "abc-def". When I am using match_phrase query for searching documents with fieldname "abc" it is returning me documents with values "abc-def" as well. However when I am querying for "abc-def" it is working fine. My query is as follows:

  Get my_index/_search
   {
     "query" : {
           "match_phrase" : {"fieldname" : "abc"}
       }
   }

Can someone please help me in understanding the issue?

1

There are 1 best solutions below

0
On BEST ANSWER
  1. match_phrase query analyzes the search term based on the analyzer provided for the field (if no analyzer is added then by default standard analyzer is used).

Match phrase query searches for those documents that have all the terms present in the field (from the search term), and the terms must be present in the correct order.

  1. In your case, "abc-def" gets tokenized to "abc" and "def" (because of standard analyzer). Now when you are using match phrase query for "abc-def", this searches for all documents that have both abc and def in the same order. (therefore you are getting only 1 doc in the result)

  2. When searching for "abc", this will search for those documents that have abc in the fieldname field (since both the document contain abc, so both are returned in the result)

If you want to return only exact matching documents in the result, then you need to change the way the terms are analyzed.

  1. If you have not explicitly defined any mapping then you need to add .keyword to the fieldname field. This uses the keyword analyzer instead of the standard analyzer (notice the ".keyword" after the fieldname field).

Adding a working example with index data, mapping, search query and search result

Index data:

{
    "name":"abc-def"
}
{
    "name":"abc"
}

Search Query:

{
  "query": {
    "match_phrase": {
      "name.keyword": "abc"
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "67394740",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.6931471,
        "_source": {
          "name": "abc"
        }
      }
    ]