Bird
0
0

How would you combine a bool query with a range filter to find documents with price between 10 and 50, must have in_stock true, and should have discount greater than 20%?

hard🚀 Application Q9 of 15
Elasticsearch - Basic Search Queries
How would you combine a bool query with a range filter to find documents with price between 10 and 50, must have in_stock true, and should have discount greater than 20%?
A{"bool": {"should": [{"range": {"price": {"gte": 10, "lte": 50}}}], "must": [{"term": {"in_stock": true}}], "filter": [{"range": {"discount": {"gt": 20}}}]}}
B{"bool": {"must": [{"range": {"price": {"gte": 10, "lte": 50}}}], "filter": [{"term": {"in_stock": true}}], "must_not": [{"range": {"discount": {"gt": 20}}}]}}
C{"bool": {"filter": [{"range": {"price": {"gte": 10, "lte": 50}}}, {"term": {"in_stock": true}}], "should": [{"range": {"discount": {"gt": 20}}}]}}
D{"bool": {"must_not": [{"range": {"price": {"gte": 10, "lte": 50}}}], "should": [{"term": {"in_stock": true}}], "filter": [{"range": {"discount": {"gt": 20}}}]}}
Step-by-Step Solution
Solution:
  1. Step 1: Use filter for price range and in_stock

    Filters are used for exact matching and range without scoring, so price range and in_stock go in filter.
  2. Step 2: Use should for discount boosting

    The discount > 20% is optional and boosts score, so it goes in should.
  3. Final Answer:

    Bool query with filter for price and in_stock, should for discount -> Option C
  4. Quick Check:

    Filter for exact/range, should for boosting [OK]
Quick Trick: Put exact and range filters in filter, optional boosts in should [OK]
Common Mistakes:
MISTAKES
  • Putting range in must_not
  • Using must for filters unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Elasticsearch Quizzes