📖 Scenario: You are building a search feature for an online bookstore. You want to find books that match a search term in either the title or the author fields. You want Elasticsearch to return the best matching score from these fields, not the sum of scores.
🎯 Goal: Create a dis_max query in Elasticsearch that searches for the term "adventure" in the title and author fields, and returns the best score among these fields.
📋 What You'll Learn
Create a dis_max query with two match queries inside it
Search for the term "adventure" in the title field
Search for the term "adventure" in the author field
Set the tie_breaker parameter to 0.3
Print the final JSON query
💡 Why This Matters
🌍 Real World
Dis max queries help when you want to search multiple fields but only want the best matching score to affect the ranking, like searching books by title or author.
💼 Career
Understanding dis_max queries is useful for building efficient and relevant search features in Elasticsearch, a popular search engine used in many companies.
Progress0 / 4 steps
1
Create the base query structure
Create a variable called query and assign it an empty dictionary with a dis_max key containing an empty dictionary.
Elasticsearch
Hint
Start by creating a dictionary named query with a dis_max key.
2
Add the queries list inside dis_max
Inside the dis_max dictionary in query, add a key called queries with an empty list as its value.
Elasticsearch
Hint
Add a queries key with an empty list inside the dis_max dictionary.
3
Add match queries for title and author
Add two match query dictionaries inside the queries list in query. The first should match the term "adventure" in the title field, and the second should match the term "adventure" in the author field.
Elasticsearch
Hint
Each query inside queries should be a dictionary with a match key.
4
Add tie_breaker and print the query
Add a tie_breaker key with the value 0.3 inside the dis_max dictionary in query. Then print the query variable.
Elasticsearch
Hint
Set tie_breaker to 0.3 and print the query dictionary.
Practice
(1/5)
1. What is the main purpose of the dis_max query in Elasticsearch?
easy
A. To select the highest score from multiple queries to find the best match
B. To combine all scores from multiple queries by summing them
C. To filter documents based on a single exact match
D. To sort documents alphabetically by a field
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 A
Quick Check:
dis_max picks highest score = A [OK]
Hint: Remember: dis_max picks the best single score, not sums [OK]
Common Mistakes:
Thinking dis_max sums scores from all queries
Confusing dis_max with filter queries
Assuming dis_max sorts documents alphabetically
2. Which of the following is the correct syntax to use a dis_max query with two match queries in Elasticsearch?
easy
A. {\"dis_max\": {\"queries\": {\"match\": {\"field1\": \"value1\"}, \"match\": {\"field2\": \"value2\"}}}}
B. {\"dis_max\": [{\"match\": {\"field1\": \"value1\"}}, {\"match\": {\"field2\": \"value2\"}}]}
C. {\"dis_max\": {\"queries\": [{\"match\": {\"field1\": \"value1\"}}, {\"match\": {\"field2\": \"value2\"}}]}}
D. {\"dis_max\": {\"query\": [{\"match\": {\"field1\": \"value1\"}}, {\"match\": {\"field2\": \"value2\"}}]}}
Solution
Step 1: Check the dis_max query structure
The dis_max query requires a queries array containing individual query objects.
Step 2: Validate the JSON syntax
{\"dis_max\": {\"queries\": [{\"match\": {\"field1\": \"value1\"}}, {\"match\": {\"field2\": \"value2\"}}]}} correctly uses queries as an array with two match queries inside separate objects.
B. The queries value should be an array, not an object; replace curly braces with square brackets
C. The match queries must be inside a 'query' key, not 'queries'
D. The field names must be strings without quotes
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 B
Quick Check:
queries must be array [] not object {} = A [OK]
Hint: Use [] for queries array, not {} [OK]
Common Mistakes:
Using curly braces instead of square brackets for queries
Adding unnecessary boost parameter causing errors
Removing quotes from field names
5. You want to boost the importance of a 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?
hard
A. { "dis_max": { "boost": 2.0, "tie_breaker": 0.5 } }
B. { "dis_max": { "queries": [...], "boost": "high" } }
C. { "dis_max": { "queries": [...], "tie_breaker": 2.0 } }
D. { "dis_max": { "queries": [...], "boost": 2.0 } }
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.