A boosting query helps you show some search results as more important and others as less important.
Boosting query in Elasticsearch
Introduction
Syntax
Elasticsearch
{
"query": {
"boosting": {
"positive": { <query> },
"negative": { <query> },
"negative_boost": <number>
}
}
}positive is the query for results you want to boost.
negative is the query for results you want to reduce in score.
Examples
Elasticsearch
{
"query": {
"boosting": {
"positive": { "match": { "title": "apple" } },
"negative": { "match": { "title": "banana" } },
"negative_boost": 0.5
}
}
}Elasticsearch
{
"query": {
"boosting": {
"positive": { "term": { "category": "electronics" } },
"negative": { "term": { "category": "outdated" } },
"negative_boost": 0.3
}
}
}Sample Program
This query boosts documents mentioning "fresh fruit" and lowers those mentioning "rotten" in the description.
Elasticsearch
{
"query": {
"boosting": {
"positive": {
"match": {
"description": "fresh fruit"
}
},
"negative": {
"match": {
"description": "rotten"
}
},
"negative_boost": 0.2
}
}
}Important Notes
The negative_boost value must be between 0 and 1. Smaller means stronger lowering.
Boosting query helps balance showing good matches while pushing down less relevant ones.
Summary
Boosting query lets you raise some results and lower others in search results.
Use positive and negative queries with a negative_boost number.
This helps make search results more useful and relevant for users.
Practice
1. What is the main purpose of a
boosting query in Elasticsearch?easy
Solution
Step 1: Understand boosting query purpose
A boosting query is designed to adjust the relevance scores of search results by raising some and lowering others.Step 2: Compare with other query types
Sorting alphabetically, filtering, and aggregating are different Elasticsearch features unrelated to boosting.Final Answer:
To increase the relevance score of some results and decrease others -> Option CQuick Check:
Boosting query = Adjust relevance scores [OK]
Hint: Boosting changes scores, not sorting or filtering [OK]
Common Mistakes:
- Confusing boosting with sorting
- Thinking boosting filters documents
- Mixing boosting with aggregation
2. Which of the following is the correct syntax snippet for a boosting query in Elasticsearch?
easy
Solution
Step 1: Identify correct boosting query structure
The correct syntax uses the "boosting" key with "positive", "negative", and "negative_boost" fields.Step 2: Check each option
The snippet using "boosting" with "positive" match query, "negative" match query, and "negative_boost": 0.5 matches the correct structure. Others miss "negative_boost" or use incorrect keys like "boost" or "boosting_query".Final Answer:
{ "boosting": { "positive": { "match": { "field": "value" } }, "negative": { "match": { "field": "other" } }, "negative_boost": 0.5 } } -> Option DQuick Check:
Boosting query syntax = { "boosting": { "positive": { "match": { "field": "value" } }, "negative": { "match": { "field": "other" } }, "negative_boost": 0.5 } } [OK]
Hint: Look for "boosting" with positive, negative, and negative_boost keys [OK]
Common Mistakes:
- Using wrong key names like "boost" or "boosting_query"
- Omitting "negative_boost"
- Using incorrect query types inside positive/negative
3. Given this boosting query, what will happen to documents matching the negative query?
{
"boosting": {
"positive": { "match": { "title": "apple" } },
"negative": { "match": { "title": "banana" } },
"negative_boost": 0.3
}
}medium
Solution
Step 1: Understand negative_boost effect
The negative_boost value multiplies the score of documents matching the negative query, lowering their relevance.Step 2: Apply to given query
Documents matching "banana" will have their scores multiplied by 0.3, reducing their rank but not excluding them.Final Answer:
Their relevance scores will be decreased by multiplying by 0.3 -> Option BQuick Check:
negative_boost multiplies score down [OK]
Hint: Negative matches get score multiplied by negative_boost < 1 [OK]
Common Mistakes:
- Thinking negative_boost adds to score
- Assuming negative matches are filtered out
- Confusing negative_boost with positive boost
4. Identify the error in this boosting query:
{
"boosting": {
"positive": { "match": { "content": "dog" } },
"negative": { "match": { "content": "cat" } }
}
}medium
Solution
Step 1: Check required fields in boosting query
A boosting query requires "positive", "negative", and "negative_boost" fields to work correctly.Step 2: Identify missing element
The provided query lacks the "negative_boost" field, which defines how much to reduce negative matches.Final Answer:
Missing "negative_boost" field -> Option AQuick Check:
Boosting query needs negative_boost [OK]
Hint: Always include negative_boost in boosting queries [OK]
Common Mistakes:
- Omitting negative_boost
- Using wrong key names
- Assuming same field usage is invalid
5. You want to boost documents containing "premium" in the
category field and reduce scores of documents containing "basic" in the same field by half. Which boosting query correctly achieves this?hard
Solution
Step 1: Identify positive and negative queries
Positive query should match "premium" category to boost, negative query should match "basic" to reduce score.Step 2: Check negative_boost value
Negative boost of 0.5 reduces scores of negative matches by half, which matches the requirement.Step 3: Verify options
{ "boosting": { "positive": { "term": { "category": "premium" } }, "negative": { "term": { "category": "basic" } }, "negative_boost": 0.5 } } correctly uses term queries and negative_boost 0.5. Other options either use negative_boost greater than 1, are not boosting queries, or reverse positive and negative.Final Answer:
{ "boosting": { "positive": { "term": { "category": "premium" } }, "negative": { "term": { "category": "basic" } }, "negative_boost": 0.5 } } -> Option AQuick Check:
Positive = premium, negative = basic, negative_boost = 0.5 [OK]
Hint: Positive boosts, negative reduces with negative_boost < 1 [OK]
Common Mistakes:
- Swapping positive and negative queries
- Using negative_boost greater than 1
- Using boost instead of boosting query
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
