0
0
Elasticsearchquery~15 mins

Date histogram in Elasticsearch - Mini Project: Build & Apply

Choose your learning style9 modes available
Date histogram
📖 Scenario: You work for a company that collects sales data with timestamps. You want to see how many sales happened each day.
🎯 Goal: Create an Elasticsearch query that groups sales by day using a date histogram aggregation.
📋 What You'll Learn
Create an Elasticsearch query with a date histogram aggregation named sales_per_day.
Use the field sale_date for the date histogram.
Set the interval to day.
Include a match_all query to get all documents.
💡 Why This Matters
🌍 Real World
Date histograms help businesses analyze trends over time, like daily sales or website visits.
💼 Career
Knowing how to write date histogram queries is useful for data analysts and backend developers working with time-series data in Elasticsearch.
Progress0 / 4 steps
1
Create the base query
Create a variable called query and set it to a JSON object with a query key containing a match_all query.
Elasticsearch
Need a hint?

The match_all query returns all documents.

2
Add the date histogram aggregation
Add an aggs key to the query variable. Inside aggs, create a date histogram aggregation named sales_per_day that uses the field sale_date and sets the interval to day.
Elasticsearch
Need a hint?

The date_histogram aggregation groups documents by date intervals.

3
Complete the query structure
Ensure the query variable is a complete JSON object with query and aggs keys as described. No changes needed if already done.
Elasticsearch
Need a hint?

Check that the query has both query and aggs keys.

4
Print the final query
Write print(query) to display the complete Elasticsearch query.
Elasticsearch
Need a hint?

Use print(query) to see the full query JSON.