When a bookstore search checks both a book’s title and synopsis, a disjunction max query lets either field match while ranking each document by its strongest field match. It is written as the compound query dis_max and wraps individual leaf queries. section 10.8.2 section 11.6
Configure the query around the scoring behavior you need:
- Put each field-specific match query inside the queries array; at least one subquery must match. section 11.6
- Use the highest matching subquery score as the document’s primary relevance score. section 10.8.2 section 11.6
- Add tie_breaker when matches in other fields should contribute; Elasticsearch multiplies non-best scores by a value from 0.0 to 1.0 and adds them to the best score. section 10.8.3 section 11.6
Search product fields with a tiebreaker
A product search can favor the strongest match for “smart tv” in type, overview, or product while still rewarding documents matching multiple fields. Set tie_breaker to 0.5 when secondary matches should have a moderate effect. section 11.6
Listing 11.22 The dis_max query in action
GET products/_search
{
"_source": ["type","overview"],
"query": {
"dis_max": { #1
"queries": [{ #2
"match": {
"type": "smart tv"
}
},
{
"match": {
"overview": "smart tv"
}
}]
}
}
}
Inspect a runnable products/_search request showing dis_max, multiple match clauses, and selected source fields. section 11.6
Listing 11.23 A dis_max query with a tiebreaker
GET products/_search
{
"_source": ["type","overview"],
"query": {
"dis_max": {
"queries": [{
"match": {
"type": "smart tv"
}
},
{
"match": {
"overview": "smart TV"
}
},
{
"match": {
"product": "smart TV"
}
}],"tie_breaker": 0.5
}
}
}
Compare the same product search with tie_breaker enabled to see how additional matching fields affect relevance. section 11.6
