elasticsearchmatchfield

Elasticsearch phrase prefix query on multiple fields


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


Solution

  • 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);