The Dis max query helps find the best matching documents by choosing the highest score from multiple queries. It is useful when you want to combine several search options but only care about the best match.
Dis max query in Elasticsearch
{
"dis_max": {
"queries": [
{ "match": { "field1": "search text" } },
{ "match": { "field2": "search text" } }
],
"tie_breaker": 0.7,
"boost": 1.2
}
}queries is a list of queries to compare.
tie_breaker adds a small score from other queries to help when multiple queries match.
{
"dis_max": {
"queries": [
{ "match": { "title": "apple" } },
{ "match": { "description": "apple" } }
]
}
}{
"dis_max": {
"queries": [
{ "match": { "title": "apple" } },
{ "match": { "description": "apple" } }
],
"tie_breaker": 0.3
}
}{
"dis_max": {
"queries": [
{ "match": { "title": "apple" } },
{ "match": { "description": "apple" } }
],
"boost": 2.0
}
}This query searches for the word 'coffee' in either the title or description fields. It picks the best score from these two matches and adds half of the other scores to help when both fields match. The whole score is then boosted by 1.1 times.
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "coffee" } },
{ "match": { "description": "coffee" } }
],
"tie_breaker": 0.5,
"boost": 1.1
}
}
}The tie_breaker value is usually between 0 and 1. It controls how much the scores from non-best queries affect the final score.
Boost increases the importance of the whole dis max query compared to other queries.
Dis max query picks the highest score from multiple queries to find the best match.
Use tie_breaker to add some influence from other matching queries.
Boost can increase the overall importance of the dis max query.