Bird
0
0

How would you write a range query to find documents where timestamp is after January 1, 2023, but before or on March 31, 2023?

hard🚀 Application Q9 of 15
Elasticsearch - Basic Search Queries
How would you write a range query to find documents where timestamp is after January 1, 2023, but before or on March 31, 2023?
A{ "query": { "range": { "timestamp": { "gte": "2023-01-01", "lt": "2023-03-31" } } } }
B{ "query": { "range": { "timestamp": { "gt": "2023-01-01", "lte": "2023-03-31" } } } }
C{ "query": { "range": { "timestamp": { "gte": "2023-01-01", "lte": "2023-03-31" } } } }
D{ "query": { "range": { "timestamp": { "gt": "2023-01-01", "lt": "2023-03-31" } } } }
Step-by-Step Solution
Solution:
  1. Step 1: Interpret the date boundaries

    'After January 1' means strictly greater than Jan 1, so use 'gt'. 'Before or on March 31' means less than or equal to March 31, so use 'lte'.
  2. Step 2: Match these operators to options

    { "query": { "range": { "timestamp": { "gt": "2023-01-01", "lte": "2023-03-31" } } } } uses 'gt' for Jan 1 and 'lte' for March 31, matching the requirement.
  3. Final Answer:

    Use gt for after Jan 1 and lte for before or on March 31 -> Option B
  4. Quick Check:

    After Jan 1 = gt, before or on Mar 31 = lte [OK]
Quick Trick: Use gt for exclusive lower, lte for inclusive upper date bounds [OK]
Common Mistakes:
MISTAKES
  • Using gte instead of gt for 'after' date
  • Using lt instead of lte for 'before or on' date
  • Confusing inclusive and exclusive operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Elasticsearch Quizzes