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.