0
0
Elasticsearchquery~30 mins

Why aggregations summarize data in Elasticsearch - See It in Action

Choose your learning style9 modes available
Why aggregations summarize data
📖 Scenario: You work at a bookstore that uses Elasticsearch to store sales data. You want to understand how many books were sold in each genre to make better stock decisions.
🎯 Goal: Build an Elasticsearch query that uses aggregations to summarize the total number of books sold per genre.
📋 What You'll Learn
Create an Elasticsearch index data structure with sample sales data
Add a filter to select only sales from the last month
Use a terms aggregation on the genre field to group sales
Use a sum aggregation on the copies_sold field to get total sales per genre
Print the aggregation results
💡 Why This Matters
🌍 Real World
Aggregations help businesses quickly summarize large amounts of data, like total sales per category, without manually counting each record.
💼 Career
Understanding aggregations is essential for data analysts and backend developers working with Elasticsearch to create reports and dashboards.
Progress0 / 4 steps
1
DATA SETUP: Create sample sales data
Create a variable called sales_data that contains a list of dictionaries. Each dictionary should have these exact keys and values: {"genre": "fiction", "copies_sold": 10}, {"genre": "non-fiction", "copies_sold": 5}, {"genre": "fiction", "copies_sold": 7}, {"genre": "science", "copies_sold": 3}.
Elasticsearch
Need a hint?

Use a list with dictionaries exactly as shown.

2
CONFIGURATION: Add a filter for recent sales
Create a variable called query that contains a dictionary with a range filter on the sale_date field to select sales from the last 30 days. Use the exact key gte with value "now-30d/d".
Elasticsearch
Need a hint?

Use a dictionary with keys range, sale_date, and gte.

3
CORE LOGIC: Build the aggregation query
Create a variable called agg_query that contains a dictionary with a terms aggregation on the genre field named genres, and inside it a sum aggregation on the copies_sold field named total_copies.
Elasticsearch
Need a hint?

Use nested dictionaries for terms and sum aggregations exactly as shown.

4
OUTPUT: Print the aggregation results
Write a print statement that outputs the string "Aggregation query:" followed by the agg_query variable.
Elasticsearch
Need a hint?

Use print("Aggregation query:", agg_query) exactly.