Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch phrase prefix query on multiple fields

Tags:

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" ?

like image 847
Gustavo Matias Avatar asked Jul 10 '13 05:07

Gustavo Matias


1 Answers

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);
like image 80
javanna Avatar answered Sep 28 '22 08:09

javanna