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?

fidans
  • 31
  • 1
  • match_phrase_prefix should return document with "stack o". Can you add your full query and mapping – jaspreet chahal Mar 18 '20 at 15:26
  • @jaspreetchahal Unfortunately, i am not allowed to share the source code. Best i can do is share this: https://i.imgur.com/pmfI89V.png – fidans Mar 19 '20 at 10:21
  • I have the same issue and seems the documentation says it should work, but read the note in the end of these articles: http://man.hubwiz.com/docset/ElasticSearch.docset/Contents/Resources/Documents/www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html and https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html and maybe it somehow connected to `max_expansions` param – Chaki_Black Aug 13 '21 at 14:12

2 Answers2

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,
            }
        }
    }
}
FBHY
  • 1,004
  • 1
  • 15
  • 35
  • 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 http://man.hubwiz.com/docset/ElasticSearch.docset/Contents/Resources/Documents/www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html which confirms the above. Would be nice to replace with the "official" Elastic documentation for same. – Bill Jan 25 '23 at 00:58
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.

Amit
  • 30,756
  • 6
  • 57
  • 88
  • 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 Mar 19 '20 at 09:54
  • @fidans, sorry I didn't get you, its working for `stack o` which a word followed by a space and then a single letter – Amit Mar 19 '20 at 09:56
  • 1
    I apologize for the inconvenience but i am not allowed to share the source code. Best i can do is visualize two examples: 1: https://i.imgur.com/pmfI89V.png 2: https://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 Mar 19 '20 at 10:30
  • @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 – Amit Mar 19 '20 at 10:41
  • The document is depended by that id. However, it appears that the issue is not completely consistent. 1: https://i.imgur.com/wu7MoLp.png 2: https://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). – fidans Mar 19 '20 at 12:25
  • @fidans, thanks for following on this, but this looks weird and its important to know your index mapping, can you share that as well, particularly the analyzer applied on your `display_search_field` – Amit Mar 19 '20 at 16:00
  • @fidans, I tried with my mapping(provided in the answer) and tested with both `freskim aliu` and `display nejmgg` and both returns result with just one char at the end. – Amit Mar 19 '20 at 16:14
  • @fidans, I just again looked at your samples and you are doing very basic mistakes and mixing several things – Amit Mar 19 '20 at 16:16
  • @fidans, please use the mapping provided by me for your `display_search_field` and reindex your data and make proper query, remember first term in your search query needs to be full and only on last term it would make a prefix query – Amit Mar 19 '20 at 16:19
  • @fidans, are you still facing issue? – Amit Mar 20 '20 at 03:28
  • Sorry for the delayed response. I do not have access to kibana to see the index mapping but i will contact the DevOps. Also, i did not write any of the queries for this and my knowledge on es is very limited, can you tell me what basic mistakes i've made, or the mixing? – fidans Mar 20 '20 at 16:02
  • @fidans, thanks for response, you can just hit the `https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html` GET mapping to see the mapping details. Also you can create your own elasticsearch index locally and use my mapping and your sample data(make usre to use the small data, so that chances of mixing things are less) and test it yourself – Amit Mar 21 '20 at 00:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210045/discussion-between-opster-elasticsearch-ninja-and-fidans). – Amit Mar 21 '20 at 04:47