Skip to main content

Multi Match Query

The MultiMatch query builds on the match query to allow multi-field queries.

In order to use the MultiMatch query import the following:

import zio.elasticsearch.query.MultiMatchQuery
import zio.elasticsearch.ElasticQuery._

You can create a MultiMatch query without specifying fields using the multiMatch method this way:

val query: MultiMatchQuery = multiMatch(value = "test")

If you want to change the fields that will be searched, you can use the fields method:

val query: MultiMatchQuery = multiMatch(value = "test").fields("stringField1", "stringField2")

If you want to change the fields that will be searched, you can use the fields method with the type-safe parameters this way:

val query: MultiMatchQuery = multiMatch(value = "test").fields(Document.stringField1, Document.stringField2)

If you want to change the boost, you can use the boost method:

val queryWithBoost: MultiMatchQuery = multiMatch(value = "test").fields(Document.stringField1, Document.stringField2).boost(2.2)

If you want to change the minimum_should_match, you can use the minimumShouldMatch method:

val queryWithMinimumShouldMatch: MultiMatchQuery = multiMatch(value = "test").fields(Document.stringField1, Document.stringField2).minimumShouldMatch(2)

The minimumShouldMatch method also accepts a MinimumShouldMatch value, which supports percentages and conditional combinations as well (e.g. MinimumShouldMatch.Percentage(75) or MinimumShouldMatch.Combination(MinimumShouldMatch.Condition(3, MinimumShouldMatch.Percentage(90)))). You can find more information about the minimum_should_match parameter here.

If you want to change the type, you can use the matchingType method:

import zio.elasticsearch.query.MultiMatchType._

val queryWithType: MultiMatchQuery = multiMatch(value = "test").fields(Document.stringField1, Document.stringField2).matchingType(MostFields)

When a MultiMatch query is used inside a Nested query, the nested path is prepended to every entry in fields, while the query text is sent unchanged:

val query: NestedQuery[Document] =
nested(path = Document.subDocumentList, query = multiMatch(value = "test").fields(SubDocument.stringField))
// "fields": ["subDocumentList.stringField"]

If no fields are set, there is nothing to prefix, so Elasticsearch falls back to the index.query.default_field setting (which defaults to all eligible fields), just as it does outside a Nested query.

You can find more information about MultiMatch query here.