Dis max query in Elasticsearch - Time & Space Complexity
We want to understand how the time taken by a Dis max query changes as we add more queries inside it.
How does the number of queries affect the work Elasticsearch does?
Analyze the time complexity of the following Dis max query.
{
"dis_max": {
"queries": [
{ "match": { "title": "apple" } },
{ "match": { "description": "apple" } },
{ "match": { "content": "apple" } }
],
"tie_breaker": 0.7
}
}
This query searches multiple fields and picks the best score among them, adding a small bonus from others.
Look at what repeats when this query runs.
- Primary operation: Running each subquery (match) on the documents.
- How many times: Once per subquery inside the "queries" list.
As we add more subqueries, Elasticsearch runs more searches to find the best score.
| Number of Subqueries (n) | Approx. Operations |
|---|---|
| 3 | 3 searches on documents |
| 10 | 10 searches on documents |
| 100 | 100 searches on documents |
Pattern observation: The work grows directly with the number of subqueries.
Time Complexity: O(n)
This means the time grows linearly with the number of subqueries inside the Dis max query.
[X] Wrong: "Adding more subqueries won't affect performance much because only the best score is used."
[OK] Correct: Even though only the best score is kept, Elasticsearch still runs all subqueries fully, so more subqueries mean more work.
Understanding how query parts add up helps you explain search performance clearly and shows you can think about scaling queries well.
What if we replaced the Dis max query with a bool query using "should" clauses? How would the time complexity change?
Practice
dis_max query in Elasticsearch?Solution
Step 1: Understand the dis_max query behavior
The dis_max query evaluates multiple queries and picks the highest score among them for each document.Step 2: Compare with other scoring methods
Unlike sum or filter queries, dis_max focuses on the best single match score, not combined or filtered results.Final Answer:
To select the highest score from multiple queries to find the best match -> Option AQuick Check:
dis_max picks highest score = A [OK]
- Thinking dis_max sums scores from all queries
- Confusing dis_max with filter queries
- Assuming dis_max sorts documents alphabetically
dis_max query with two match queries in Elasticsearch?Solution
Step 1: Check the dis_max query structure
The dis_max query requires aqueriesarray containing individual query objects.Step 2: Validate the JSON syntax
{\"dis_max\": {\"queries\": [{\"match\": {\"field1\": \"value1\"}}, {\"match\": {\"field2\": \"value2\"}}]}} correctly usesqueriesas an array with two match queries inside separate objects.Final Answer:
{"dis_max": {"queries": [{"match": {"field1": "value1"}}, {"match": {"field2": "value2"}}]}} -> Option CQuick Check:
dis_max uses queries array = D [OK]
- Using 'query' instead of 'queries' key
- Passing queries as an object instead of an array
- Omitting the array brackets around queries
dis_max with tie_breaker set to 0.3:{
"dis_max": {
"queries": [
{ "match": { "title": "apple" } },
{ "match": { "description": "apple" } }
],
"tie_breaker": 0.3
}
}What effect does the
tie_breaker parameter have on the scoring?Solution
Step 1: Understand tie_breaker in dis_max
The tie_breaker adds a fraction of the scores from other matching queries to the highest score.Step 2: Apply the 0.3 value meaning
With tie_breaker 0.3, 30% of the other query scores are added to the highest score, boosting documents matching multiple queries.Final Answer:
It adds 30% of the scores from non-highest matching queries to the final score -> Option AQuick Check:
tie_breaker 0.3 adds 30% other scores = C [OK]
- Thinking tie_breaker multiplies highest score directly
- Assuming tie_breaker ignores other queries
- Believing tie_breaker subtracts from scores
dis_max query but it returns an error:{
"dis_max": {
"queries": {
{ "match": { "field1": "value1" } },
{ "match": { "field2": "value2" } }
}
}
}What is the error and how to fix it?
Solution
Step 1: Identify the queries structure error
The queries key must hold an array of query objects, but curly braces {} were used, making it an object.Step 2: Correct the syntax
Replace the curly braces around the queries with square brackets [] to form a valid array.Final Answer:
The queries value should be an array, not an object; replace curly braces with square brackets -> Option BQuick Check:
queries must be array [] not object {} = A [OK]
- Using curly braces instead of square brackets for queries
- Adding unnecessary boost parameter causing errors
- Removing quotes from field names
dis_max query compared to other queries in your Elasticsearch search. Which is the correct way to apply a boost to the entire dis_max query?Solution
Step 1: Understand boost usage in dis_max
The boost parameter is applied at the dis_max query level to increase its overall importance.Step 2: Validate correct boost syntax
Boost must be a numeric value; { "dis_max": { "queries": [...], "boost": 2.0 } } correctly places boost: 2.0 inside dis_max alongside queries.Final Answer:
{ "dis_max": { "queries": [...], "boost": 2.0 } } -> Option DQuick Check:
Boost is numeric and inside dis_max = B [OK]
- Using non-numeric boost values like strings
- Placing boost outside dis_max query
- Confusing tie_breaker with boost
Ready to prove it?
Pass the test and this pattern moves unknown
