0
0
Elasticsearchquery~30 mins

Nested aggregations in Elasticsearch - Mini Project: Build & Apply

Choose your learning style9 modes available
Nested aggregations
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print() to output the full JSON query object.