0
0
Elasticsearchquery~10 mins

Why compound queries combine conditions in Elasticsearch - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why compound queries combine conditions
Start: Define compound query
Add multiple conditions
Combine conditions using AND/OR/NOT
Execute query
Return documents matching combined conditions
Compound queries combine multiple conditions to filter documents by applying AND, OR, or NOT logic before returning matching results.
Execution Sample
Elasticsearch
{
  "bool": {
    "must": [
      { "match": { "title": "python" } },
      { "range": { "year": { "gte": 2020 } } }
    ]
  }
}
This query finds documents where the title contains 'python' AND the year is 2020 or later.
Execution Table
StepCondition EvaluatedResult for Document AResult for Document BCombined Result
1title contains 'python'TrueFalse
2year >= 2020TrueTrue
3Combine with ANDDocument A: True AND True = True Document B: False AND True = False
4Return matching documentsDocument A includedDocument B excluded
💡 Query returns only documents where all conditions in 'must' are true.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Document A title matchUnknownTrueTrueTrue
Document A year checkUnknownUnknownTrueTrue
Document B title matchUnknownFalseFalseFalse
Document B year checkUnknownUnknownTrueFalse (due to AND)
Key Moments - 2 Insights
Why does Document B get excluded even though it meets the year condition?
Because the compound query uses AND (must), all conditions must be true. Document B fails the title match (Step 1), so combined result is false (Step 3).
What happens if we use OR instead of AND to combine conditions?
Using OR (should) means documents matching any condition are included. So Document B would be included if it matches the year condition.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the combined result for Document A at Step 3?
AUnknown
BTrue
CFalse
DError
💡 Hint
Check Step 3 row under Combined Result column for Document A.
At which step does the query decide to exclude Document B?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at when combined conditions are evaluated in the execution table.
If we change 'must' to 'should' in the query, what happens to Document B?
AIt causes an error
BIt is excluded because it fails title match
CIt is included because it matches one condition
DIt is included only if both conditions match
💡 Hint
Recall key moment about OR (should) logic allowing any condition to match.
Concept Snapshot
Compound queries combine multiple conditions using AND (must), OR (should), or NOT (must_not).
All conditions in 'must' must be true for a document to match.
'Should' means any condition true includes the document.
This lets you filter documents precisely by combining criteria.
Example: title contains 'python' AND year >= 2020.
Full Transcript
Compound queries in Elasticsearch combine multiple conditions to filter documents. They use logical operators like AND (must), OR (should), and NOT (must_not). For example, a query with 'must' conditions requires all conditions to be true for a document to match. In the sample query, documents must have a title containing 'python' and a year greater or equal to 2020. The execution table shows step-by-step evaluation: first checking each condition per document, then combining results with AND. Document A matches both conditions and is included. Document B fails the title condition and is excluded. Changing 'must' to 'should' would include documents matching any condition. This approach helps build precise searches by combining multiple filters.