2

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?

3

2 Answers 2

1

You should try increasing the value of max_expansions (default 50, here)
Be careful not to put it too high either, at the risk of slowing down the query!

So you can try this:

{
    "query": {
        "match_phrase_prefix": {
            "display_name": {
                "query": "stack o",
                "max_expansions": 100,
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes Thank You @FBHY!! That is what worked for me. I think the idea is that when it sees a prefix, it tries to find all words in the inverted index that match that prefix UP TO A LIMIT. That limit is max_expansions. The bigger it is, the presumably slower it is. Someone, I think it was @Chaki_Black pointed to man.hubwiz.com/docset/ElasticSearch.docset/Contents/Resources/… which confirms the above. Would be nice to replace with the "official" Elastic documentation for same.
0

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.

13 Comments

Beside the value ( in this case "stack o") which i have to convert to lowerCase, the rest of the code is mostly similar if not identical. The problem only occurs when i try a word followed by a space and then a single letter ("stack o", "abc d" etc.). It works completely fine when other letters are added, Is there any general case where this could happen? because this seems like a very odd problem.
@fidans, sorry I didn't get you, its working for stack o which a word followed by a space and then a single letter
I apologize for the inconvenience but i am not allowed to share the source code. Best i can do is visualize two examples: 1: i.imgur.com/pmfI89V.png 2: i.imgur.com/yrA37re.png The full display name is 'ali mokaj'. The first case doesn't work, while the second one and any other case does.
@fidans, we don't need your source code to debug the issue, can u just remove another term query for id also post the document containg ali mokaj ? also search response in both cases, like I provided in my answer
The document is depended by that id. However, it appears that the issue is not completely consistent. 1: i.imgur.com/wu7MoLp.png 2: i.imgur.com/Qn02SQn.png First case is similar to the previous example, searching with 'freskim a' wouldn't work while searching with 'freskim al' produced the first response (first link). Second case however gave the response by querying 'display n' (second link).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.