📖 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
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
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
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
Hint
Use print("Aggregation query:", agg_query) exactly.
Practice
(1/5)
1. What is the main purpose of aggregations in Elasticsearch?
easy
A. To delete documents from the index
B. To summarize data like totals, averages, and counts
C. To update the mapping of fields
D. To create new indexes
Solution
Step 1: Understand what aggregations do
Aggregations are used to get summaries such as totals, averages, and counts from data.
Step 2: Compare with other options
Deleting documents, updating mappings, and creating indexes are not related to summarizing data.
Final Answer:
To summarize data like totals, averages, and counts -> Option B
Quick Check:
Aggregations summarize data = A [OK]
Hint: Aggregations summarize data, not modify it [OK]
Common Mistakes:
Confusing aggregations with data deletion
Thinking aggregations change index structure
Mixing aggregations with indexing operations
2. Which part of an Elasticsearch query contains aggregations?
easy
A. aggs
B. query
C. filter
D. sort
Solution
Step 1: Identify the query structure
Elasticsearch queries have different parts like query, aggs, filter, and sort.
Step 2: Locate where aggregations go
Aggregations are always written inside the aggs section to summarize data.
Final Answer:
aggs -> Option A
Quick Check:
Aggregations inside aggs = A [OK]
Hint: Aggregations live inside the aggs section [OK]
Common Mistakes:
Putting aggregations inside query or filter
Confusing aggs with sort
Missing the aggs keyword
3. Given this aggregation query snippet, what does it calculate?