Bird
Raised Fist0
Elasticsearchquery~10 mins

Boosting query in Elasticsearch - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Boosting query
Start: Input Query
↓
Define Positive Query
↓
Define Negative Query
↓
Set Boost Factor
↓
Execute Boosting Query
↓
Results with Scores Adjusted
↓
End
The boosting query runs a positive query to find relevant documents, then reduces scores of documents matching a negative query by a boost factor, returning adjusted results.
Execution Sample
Elasticsearch
{
  "query": {
    "boosting": {
      "positive": { "match": { "title": "apple" } },
      "negative": { "match": { "title": "fruit" } },
      "negative_boost": 0.5
    }
  }
}
This query finds documents with 'apple' in the title but lowers scores for those also containing 'fruit' by half.
Execution Table
StepActionQuery PartDocuments MatchedScore Effect
1Run positive querymatch title: appleDoc1, Doc2, Doc3Base scores assigned
2Run negative querymatch title: fruitDoc2, Doc4Identify docs to reduce score
3Apply negative boostnegative_boost: 0.5Doc2Score of Doc2 multiplied by 0.5
4Combine scoresboosting queryDoc1, Doc2, Doc3Doc1 and Doc3 keep base score; Doc2 score reduced
5Return resultsfinal outputDoc1, Doc2, Doc3Sorted by adjusted scores
6End--Execution complete
💡 All documents matching positive query processed; negative boost applied to overlapping docs; results returned.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
positive_docs[][Doc1, Doc2, Doc3][Doc1, Doc2, Doc3][Doc1, Doc2, Doc3][Doc1, Doc2, Doc3]
negative_docs[][][Doc2, Doc4][Doc2, Doc4][Doc2, Doc4]
scores{}{Doc1:1.0, Doc2:1.0, Doc3:1.0}{Doc1:1.0, Doc2:1.0, Doc3:1.0}{Doc1:1.0, Doc2:0.5, Doc3:1.0}{Doc1:1.0, Doc2:0.5, Doc3:1.0}
Key Moments - 3 Insights
Why does Doc2's score get reduced but Doc4 is not in the final results?
Doc2 matches both positive and negative queries, so its score is reduced by the negative boost (see execution_table step 3). Doc4 only matches the negative query, so it is excluded because it doesn't match the positive query (step 1).
What happens if the negative_boost is set to 1?
If negative_boost is 1, scores of documents matching the negative query are multiplied by 1, so their scores stay the same (no reduction). This means the boosting query behaves like a normal positive query without score reduction (refer to execution_table step 3).
Can documents not matching the positive query appear in results?
No, only documents matching the positive query are considered for results. Negative query only reduces scores of these documents if they also match the negative query (see execution_table steps 1 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the score of Doc2 after applying the negative boost?
A1.0
B0.0
C0.5
D2.0
💡 Hint
Check execution_table row 3 where negative boost is applied to Doc2's score.
At which step does the query exclude documents that do not match the positive query?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look at execution_table step 1 where positive query matches documents.
If negative_boost was set to 0, what would happen to the scores of documents matching the negative query?
AScores would be doubled
BScores would be zeroed out
CScores would be unchanged
DDocuments would be removed
💡 Hint
Negative boost multiplies scores; 0 means scores become zero (see execution_table step 3 logic).
Concept Snapshot
Boosting query syntax:
{
  "query": {
    "boosting": {
      "positive": {...},
      "negative": {...},
      "negative_boost": 0.5
    }
  }
}

Behavior:
- Finds docs matching positive query
- Reduces scores of docs matching negative query by negative_boost
- Returns results sorted by adjusted scores

Key rule: Only docs matching positive query appear; negative query lowers their scores.
Full Transcript
A boosting query in Elasticsearch runs two queries: a positive query to find relevant documents and a negative query to identify documents whose scores should be reduced. The negative_boost factor multiplies the scores of documents matching the negative query, lowering their rank. Documents not matching the positive query are excluded. This process helps prioritize documents matching the positive query but penalizes those also matching the negative query. The execution table shows step-by-step how documents are matched and scores adjusted. The variable tracker follows the sets of documents and their scores through each step. Key moments clarify common confusions about which documents appear and how scores change. The visual quiz tests understanding of score changes and query behavior. The concept snapshot summarizes the syntax and main behavior of the boosting query.

Practice

(1/5)
1. What is the main purpose of a boosting query in Elasticsearch?
easy
A. To filter out documents without a specific field
B. To sort results alphabetically
C. To increase the relevance score of some results and decrease others
D. To aggregate data by terms

Solution

  1. 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.
  2. Step 2: Compare with other query types

    Sorting alphabetically, filtering, and aggregating are different Elasticsearch features unrelated to boosting.
  3. Final Answer:

    To increase the relevance score of some results and decrease others -> Option C
  4. Quick 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
A. { "boosting": { "positive": { "term": { "field": "value" } }, "negative": { "term": { "field": "other" } } } }
B. { "boost": { "match": { "field": "value" }, "boost_factor": 2 } }
C. { "boosting_query": { "positive": { "term": { "field": "value" } }, "negative": { "term": { "field": "other" } } } }
D. { "boosting": { "positive": { "match": { "field": "value" } }, "negative": { "match": { "field": "other" } }, "negative_boost": 0.5 } }

Solution

  1. Step 1: Identify correct boosting query structure

    The correct syntax uses the "boosting" key with "positive", "negative", and "negative_boost" fields.
  2. 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".
  3. Final Answer:

    { "boosting": { "positive": { "match": { "field": "value" } }, "negative": { "match": { "field": "other" } }, "negative_boost": 0.5 } } -> Option D
  4. Quick 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
A. Their relevance scores will be increased by 0.3
B. Their relevance scores will be decreased by multiplying by 0.3
C. They will be excluded from results
D. They will have the same score as positive matches

Solution

  1. Step 1: Understand negative_boost effect

    The negative_boost value multiplies the score of documents matching the negative query, lowering their relevance.
  2. Step 2: Apply to given query

    Documents matching "banana" will have their scores multiplied by 0.3, reducing their rank but not excluding them.
  3. Final Answer:

    Their relevance scores will be decreased by multiplying by 0.3 -> Option B
  4. Quick 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
A. Missing "negative_boost" field
B. Incorrect query type inside positive
C. Wrong key name "boosting" should be "boost"
D. Positive and negative queries cannot use the same field

Solution

  1. Step 1: Check required fields in boosting query

    A boosting query requires "positive", "negative", and "negative_boost" fields to work correctly.
  2. Step 2: Identify missing element

    The provided query lacks the "negative_boost" field, which defines how much to reduce negative matches.
  3. Final Answer:

    Missing "negative_boost" field -> Option A
  4. Quick 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
A. { "boosting": { "positive": { "term": { "category": "premium" } }, "negative": { "term": { "category": "basic" } }, "negative_boost": 0.5 } }
B. { "boosting": { "positive": { "match": { "category": "premium" } }, "negative": { "match": { "category": "basic" } }, "negative_boost": 2 } }
C. { "boost": { "term": { "category": "premium" }, "boost_factor": 2 } }
D. { "boosting": { "positive": { "term": { "category": "basic" } }, "negative": { "term": { "category": "premium" } }, "negative_boost": 0.5 } }

Solution

  1. Step 1: Identify positive and negative queries

    Positive query should match "premium" category to boost, negative query should match "basic" to reduce score.
  2. Step 2: Check negative_boost value

    Negative boost of 0.5 reduces scores of negative matches by half, which matches the requirement.
  3. 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.
  4. Final Answer:

    { "boosting": { "positive": { "term": { "category": "premium" } }, "negative": { "term": { "category": "basic" } }, "negative_boost": 0.5 } } -> Option A
  5. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard