Reputation: 5298
In Elasticsearch, I would like to match the record "John Oxford" when searching "John Ox". I'm currently using a match_phrase_prefix
as such:
{
"query": {
"match_phrase_prefix":{
"SearchName": {
"query": "John Ox"
}
}
}
}
I know this doesn't work because, as the docs state:
While easy to set up, using the match_phrase_prefix query for search autocompletion can sometimes produce confusing results.
For example, consider the query string quick brown f. This query works by creating a phrase query out of quick and brown (i.e. the term quick must exist and must be followed by the term brown). Then it looks at the sorted term dictionary to find the first 50 terms that begin with f, and adds these terms to the phrase query.
The problem is that the first 50 terms may not include the term fox so the phrase quick brown fox will not be found. This usually isn’t a problem as the user will continue to type more letters until the word they are looking for appears.
For better solutions for search-as-you-type see the completion suggester and the search_as_you_type field type.
Is there another way to achieve this, then, without changing the way the data is stored in ES?
Upvotes: 0
Views: 305
Reputation: 16182
You can use match bool prefix query. Adding a working example with index data and search query
Index Data:
{
"name":"John Oxford"
}
Search Query:
{
"query": {
"match_bool_prefix" : {
"name" : "John Ox"
}
}
}
Search Result:
"hits" : [
{
"_index" : "idx",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.287682,
"_source" : {
"name" : "John Oxford"
}
}
]
Upvotes: 0