Elasticsearch phrase prefix query on multiple fields
Asked Answered
T

2

20

I'm new to ES and I'm trying to build a query that would use phrase_prefix for multiple fields so I dont have to search more than once.

Here's what I've got so far:

{ 
    "query" : { 
        "text" : { 
            "first_name" : { 
                "query" : "Gustavo", 
                "type" : "phrase_prefix" 
            }
        } 
    }
}'

Does anybody knows how to search for more than one field, say "last_name" ?

Thuythuya answered 10/7, 2013 at 5:23 Comment(0)
J
52

The text query that you are using has been deprecated (effectively renamed) a while ago in favour of the match query. The match query supports a single field, but you can use the multi_match query which supports the very same options and allows to search on multiple fields. Here is an example that should be helpful to you:

{
    "query" : {
        "multi_match" : {
            "fields" : ["title", "subtitle"],
            "query" : "trying out ela",
            "type" : "phrase_prefix"
        }
    }
}

You can achieve the same using the Java API like this:

QueryBuilders.multiMatchQuery("trying out ela", "title", "subtitle")
    .type(MatchQueryBuilder.Type.PHRASE_PREFIX);
Johanna answered 12/7, 2013 at 11:52 Comment(4)
awesome! thank you very much! by the way, do you know by any chance how to accomplish this in Java? I can't find in the docs.. rajish.github.io/api/elasticsearch/0.20.0.Beta1-SNAPSHOT/org/…, java.lang.String...)Thuythuya
Thanks,w orks nicely! For me, it made sense to use/add "fields" : [ "*" ], "lenient" : true so that all fields are searched, and text entry does not cause Exceptions when a numeric field is (implicitly) included in the field list.Plutonic
The above didn't give me phrase prefix I don't thinkWalhalla
@Plutonic thanks, your suggestion worked. Maybe it should be on its own anwser :)Shauna
M
0

If you realize some results are missing for phrase_prefix search, it's because of the max_expansions limitation.

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.

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html#match-phrase-prefix-autocomplete


As a workaround you can use multiple prefix under a bool query.

GET items/_search
{"query":{"bool":{"should":[
  {"prefix":{"name_en":{"value":"musab","case_insensitive":true}}},
  {"prefix":{"name_tr":{"value":"musab","case_insensitive":true}}},
  {"prefix":{"name_de":{"value":"musab","case_insensitive":true}}}
]}}}
Mercurous answered 7/8, 2024 at 10:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.