Nested and Sub-Aggregations
Learn to combine aggregations by nesting metrics inside buckets, building multi-level analytics, and traversing nested document structures with the nested aggregation.
Nested and Sub-Aggregations is a free Elasticsearch & Full Text Search Systems lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Elasticsearch & Full Text Search Systems learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Combining Aggregations
A single aggregation answers one question. Real analytics often need layered answers: average price per category, or top brands per region. Elasticsearch lets you nest aggregations inside one another.
This lesson shows how to compose them.
The aggs Hierarchy
Any bucket aggregation can contain a sub-aggs block. The sub-aggregation runs once per bucket, operating only on the documents in that bucket.
This is the core mechanism for multi-dimensional analytics.
Metric Inside a Bucket
Here we group documents by category, then compute the average price within each group. The metric aggregation lives inside the bucket aggregation's aggs.
GET sales/_search
{
"size": 0,
"aggs": {
"by_category": {
"terms": { "field": "category" },
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
}
}
}Reading the Result
Each bucket in by_category now carries an avg_price value alongside its doc_count. You get the category name, how many docs it holds, and its average price in one response.
Bucket Inside a Bucket
You can nest bucket aggregations too. Group by region, then by brand within each region. This produces a two-level breakdown.
"aggs": {
"by_region": {
"terms": { "field": "region" },
"aggs": {
"by_brand": { "terms": { "field": "brand" } }
}
}
}Mind the Cardinality
Deeply nested terms aggregations can explode combinatorially. A region with 50 brands across 20 regions yields up to 1,000 buckets. Use the size parameter to limit returned buckets and protect memory.
Multiple Sub-Aggregations
A bucket can hold several sub-aggregations at once. Here each category reports both its average and maximum price.
"by_category": {
"terms": { "field": "category" },
"aggs": {
"avg_price": { "avg": { "field": "price" } },
"max_price": { "max": { "field": "price" } }
}
}The nested Aggregation
When data uses the nested field type, you must enter that scope with a nested aggregation before aggregating on its inner fields. It points at a path.
"aggs": {
"variants": {
"nested": { "path": "variants" },
"aggs": {
"avg_qty": { "avg": { "field": "variants.quantity" } }
}
}
}Reverse Nested
Inside a nested aggregation you can jump back to the parent document scope using reverse_nested. This lets you count distinct parent docs that contain a matching nested element.
"reverse_nested": {},
"aggs": {
"parent_count": { "value_count": { "field": "_id" } }
}Sorting Buckets by a Metric
Order parent buckets by a nested sub-metric using order. Here categories are sorted by their average price, descending.
"terms": {
"field": "category",
"order": { "avg_price": "desc" }
}Design Tips
Keep nesting shallow when possible, always set size on inner terms, and put the most selective bucket aggregation first to reduce the work done by deeper levels.
Quick Check
Test your grasp of sub-aggregations.
Recap
You learned to compose aggregations:
- Any bucket aggregation can hold sub-
aggsthat run per bucket. - Nest metrics in buckets, or buckets in buckets for multi-level breakdowns.
- Use the
nestedandreverse_nestedaggregations to traverse nested document scopes. - Control bucket explosion with
sizeand order buckets by sub-metrics withorder.
Frequently asked questions
Is the “Nested and Sub-Aggregations” lesson free?
Yes — the full text of “Nested and Sub-Aggregations” is free to read here on the web, and the Elasticsearch & Full Text Search Systems course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Elasticsearch & Full Text Search Systems course, upgrade to CoddyKit PRO.
What will I learn in “Nested and Sub-Aggregations”?
Learn to combine aggregations by nesting metrics inside buckets, building multi-level analytics, and traversing nested document structures with the nested aggregation. You practise Elasticsearch & Full Text Search Systems with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Elasticsearch & Full Text Search Systems?
No prior experience is required. Elasticsearch & Full Text Search Systems on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Nested and Sub-Aggregations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Elasticsearch & Full Text Search Systems lesson?
Yes. Every Elasticsearch & Full Text Search Systems lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Metric Aggregations
- Bucket Aggregations
- Pipeline Aggregations
- Nested and Sub-Aggregations