What if you could get deep insights from your data with just one simple query?
Why Nested aggregations in Elasticsearch? - Purpose & Use Cases
Imagine you have a big list of sales data with many details, like products sold, regions, and dates. You want to find out how many products were sold in each region, and then for each region, see the total sales per product category.
Doing this by hand means scanning the entire list multiple times, grouping data manually, and calculating totals for each group. This is slow, confusing, and easy to make mistakes, especially when the data grows large.
Nested aggregations let you ask Elasticsearch to group data step-by-step in one go. You can first group by region, then inside each region group by product category, and get totals automatically. It saves time and avoids errors.
Scan all sales -> group by region -> for each region, group by category -> sum salesPOST /sales/_search
{
"size": 0,
"aggs": {
"by_region": {
"terms": {
"field": "region.keyword"
},
"aggs": {
"by_category": {
"terms": {
"field": "category.keyword"
},
"aggs": {
"total_sales": {
"sum": {
"field": "sales_amount"
}
}
}
}
}
}
}
}Nested aggregations let you explore complex data relationships quickly and accurately with just one query.
A store manager can instantly see which product categories sell best in each city, helping decide where to stock more items.
Manual grouping of complex data is slow and error-prone.
Nested aggregations perform multi-level grouping in one query.
This makes data analysis faster, simpler, and more reliable.
Practice
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 BQuick Check:
Nested aggregations = multi-level grouping [OK]
- Confusing nested aggregations with filters
- Thinking nested aggregations sort data
- Assuming nested aggregations update documents
Solution
Step 1: Identify correct nested aggregation structure
Nested aggregations are placed inside the "aggs" key of a parent aggregation.Step 2: Check "aggs": { "country": { "terms": { "field": "country" }, "aggs": { "city": { "terms": { "field": "city" } } } } } structure
"aggs": { "country": { "terms": { "field": "country" }, "aggs": { "city": { "terms": { "field": "city" } } } } } shows a terms aggregation on "country" with a nested terms aggregation on "city" inside its "aggs" key, which is correct syntax.Final Answer:
"aggs": { "country": { "terms": { "field": "country" }, "aggs": { "city": { "terms": { "field": "city" } } } } } -> Option AQuick Check:
Nested aggs inside parent's "aggs" key = "aggs": { "country": { "terms": { "field": "country" }, "aggs": { "city": { "terms": { "field": "city" } } } } } [OK]
- Placing multiple "aggs" keys at the same level
- Using "nested" keyword incorrectly
- Missing the "aggs" key inside parent aggregation
{
"aggs": {
"by_country": {
"terms": { "field": "country" },
"aggs": {
"by_city": {
"terms": { "field": "city" }
}
}
}
}
}What does the nested aggregation
by_city do?Solution
Step 1: Analyze the outer aggregation
"by_country" groups documents by the "country" field.Step 2: Analyze the nested aggregation
Inside each country group, "by_city" groups documents by the "city" field.Final Answer:
Groups documents by city inside each country group -> Option AQuick Check:
Nested terms aggregation groups inside parent group [OK]
- Thinking nested aggs filter data
- Assuming nested aggs sort data
- Ignoring the parent aggregation grouping
{
"aggs": {
"countries": {
"terms": { "field": "country" },
"terms": {
"field": "city"
}
}
}
}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 DQuick Check:
Nested aggs require "aggs" key inside parent [OK]
- Repeating aggregation type keys at same level
- Not nesting inside "aggs" key
- Confusing aggregation names with field names
Solution
Step 1: Identify the required nesting order
We want to group first by region, then inside each region by category, then inside each category by brand.Step 2: Check each option's structure
{ "aggs": { "region": { "terms": { "field": "region" }, "aggs": { "category": { "terms": { "field": "category" }, "aggs": { "brand": { "terms": { "field": "brand" } } } } } } } correctly nests "category" inside "region"'s "aggs", and "brand" inside "category"'s "aggs". Others either flatten or reverse order.Final Answer:
{ "aggs": { "region": { "terms": { "field": "region" }, "aggs": { "category": { "terms": { "field": "category" }, "aggs": { "brand": { "terms": { "field": "brand" } } } } } } } -> Option CQuick Check:
Nested aggs inside parent's "aggs" in correct order = { "aggs": { "region": { "terms": { "field": "region" }, "aggs": { "category": { "terms": { "field": "category" }, "aggs": { "brand": { "terms": { "field": "brand" } } } } } } } [OK]
- Placing all aggregations at top level without nesting
- Repeating aggregation keys at same level
- Reversing the desired grouping order
Ready to prove it?
Pass the test and this pattern moves unknown
