0
0
Elasticsearchquery~5 mins

Dis max query in Elasticsearch

Choose your learning style9 modes available
Introduction

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.

When searching for a word that might appear in different fields and you want the best match from any field.
When combining different search queries but want to avoid adding their scores together.
When you want to boost the score of the best matching query without mixing scores from others.
Syntax
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.

Examples
Basic dis max query searching 'apple' in title or description fields.
Elasticsearch
{
  "dis_max": {
    "queries": [
      { "match": { "title": "apple" } },
      { "match": { "description": "apple" } }
    ]
  }
}
Dis max query with a tie_breaker to add some score from other matches.
Elasticsearch
{
  "dis_max": {
    "queries": [
      { "match": { "title": "apple" } },
      { "match": { "description": "apple" } }
    ],
    "tie_breaker": 0.3
  }
}
Dis max query with a boost to increase the overall score of the best match.
Elasticsearch
{
  "dis_max": {
    "queries": [
      { "match": { "title": "apple" } },
      { "match": { "description": "apple" } }
    ],
    "boost": 2.0
  }
}
Sample Program

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.

Elasticsearch
{
  "query": {
    "dis_max": {
      "queries": [
        { "match": { "title": "coffee" } },
        { "match": { "description": "coffee" } }
      ],
      "tie_breaker": 0.5,
      "boost": 1.1
    }
  }
}
OutputSuccess
Important Notes

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.

Summary

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.