Problem with 'match_phrase_prefix' query for elastic search

Viewed 422

I have an issue with querying using match_phrase_prefix. P.ex let's say i have a record with display_name = "stack overflow". If i query using "stack" or "stack over" it will find the record but not if i try "stack o". I noticed this has been asked before and the issue is with the prefix but i didn't seem to find a proper answer. Any thoughts?

1 Answers

Its returns the documents for stack o, you can follow below example to see it.

Index mapping

{
  "mappings": {
    "properties": {
      "display_name": {
        "type": "text"
      }
    }
  }
}

Index document

{
   "display_name" : "stack overflow"
}

Search query

{
    "query": {
        "match_phrase_prefix" : {
            "display_name" : {
                "query" : "stack o"
            }
        }
    }
}

And it returns the above-indexed doc

"hits": [
         {
            "_index": "so-60620921-match-prefix",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.5753642,
            "_source": {
               "display_name": "stack overflow"
            }
         }
      ]

You can even check the official ES doc example where it returns the document for quick brown f.

Related