📖 Scenario: You work for a bookstore that wants to analyze sales data stored in Elasticsearch. The data includes books with their genres and sales numbers. You want to find out the total sales per genre and also the average sales per book within each genre.
🎯 Goal: Build an Elasticsearch query using nested aggregations to calculate total sales per genre and average sales per book within each genre.
📋 What You'll Learn
Create a terms aggregation on the genre field named genres
Inside genres, create a sum aggregation on the sales field named total_sales
Inside genres, create an average aggregation on the sales field named avg_sales_per_book
Output the complete JSON query with nested aggregations
💡 Why This Matters
🌍 Real World
Nested aggregations help businesses analyze grouped data with multiple metrics in Elasticsearch, such as sales by category with totals and averages.
💼 Career
Understanding nested aggregations is important for roles like data analyst, backend developer, or Elasticsearch engineer working with search and analytics.
Progress0 / 4 steps
1
Create the base aggregation by genre
Create an Elasticsearch aggregation named genres using terms on the field genre.
Elasticsearch
Hint
Use terms aggregation on genre inside aggs.
2
Add total sales sum aggregation inside genres
Inside the genres aggregation, add a sum aggregation named total_sales on the field sales.
Elasticsearch
Hint
Inside genres, add aggs with total_sales sum aggregation on sales.
3
Add average sales per book aggregation inside genres
Inside the genres aggregation, add an avg aggregation named avg_sales_per_book on the field sales alongside total_sales.
Elasticsearch
Hint
Add avg_sales_per_book average aggregation on sales inside genres alongside total_sales.
4
Output the complete nested aggregation query
Print the complete Elasticsearch query JSON with nested aggregations for genres, total_sales, and avg_sales_per_book.
Elasticsearch
Hint
Use print() to output the full JSON query object.
Practice
(1/5)
1. What is the main purpose of nested aggregations in Elasticsearch?
easy
A. To sort documents by a single field
B. To group data inside other groups for multi-level analysis
C. To filter documents based on a condition
D. To update documents in bulk
Solution
Step 1: Recall aggregation basics
Aggregations group data to summarize or analyze it.
Step 2: Examine nested aggregations
Nested aggregations group data inside other groups for detailed multi-level insights.
Final Answer:
To group data inside other groups for multi-level analysis -> Option B
Quick Check:
Nested aggregations = multi-level grouping [OK]
Hint: Nested aggs always use 'aggs' inside another aggregation [OK]
Common Mistakes:
Confusing nested aggregations with filters
Thinking nested aggregations sort data
Assuming nested aggregations update documents
2. Which of the following is the correct syntax to add a nested aggregation inside a terms aggregation in Elasticsearch?
A. Using "terms" twice at the same level is correct
B. Aggregation names cannot be plural
C. Field names "country" and "city" must be the same
D. Missing nested "aggs" key for the city aggregation
Solution
Step 1: Check aggregation structure
Each aggregation can have only one aggregation type key like "terms" at its level.
Step 2: Identify nested aggregation placement
Nested aggregations must be inside an "aggs" key, but here "terms" is repeated instead of nesting inside "aggs".
Final Answer:
Missing nested "aggs" key for the city aggregation -> Option D
Quick Check:
Nested aggs require "aggs" key inside parent [OK]
Hint: Nested aggs must be inside "aggs", not repeated aggregation keys [OK]
Common Mistakes:
Repeating aggregation type keys at same level
Not nesting inside "aggs" key
Confusing aggregation names with field names
5. You want to analyze sales data by region, then by product category, and finally by brand using nested aggregations. Which of the following query structures correctly implements this three-level nested aggregation?