Agregacje zagnieżdżone i podagregacje
Nauczą się Państwo łączyć agregacje, zagnieżdżając miary w bucketach, budując wielopoziomowe analizy i przechodząc przez zagnieżdżone struktury dokumentów za pomocą agregacji nested.
Agregacje zagnieżdżone i podagregacje to bezpłatna lekcja Elasticsearch & Full Text Search Systems na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Elasticsearch & Full Text Search Systems, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Elasticsearch & Full Text Search Systems zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Agregacje zagnieżdżone i podagregacje” jest bezpłatna?
Tak — pełny tekst „Agregacje zagnieżdżone i podagregacje” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Elasticsearch & Full Text Search Systems, przejdź na CoddyKit PRO. Kurs Elasticsearch & Full Text Search Systems zawiera 4 lekcji w sumie.
Co nauczysz się w „Agregacje zagnieżdżone i podagregacje”?
Nauczą się Państwo łączyć agregacje, zagnieżdżając miary w bucketach, budując wielopoziomowe analizy i przechodząc przez zagnieżdżone struktury dokumentów za pomocą agregacji nested. Ćwiczysz Elasticsearch & Full Text Search Systems z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Elasticsearch & Full Text Search Systems?
Nie wymagamy żadnego doświadczenia. Elasticsearch & Full Text Search Systems w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Agregacje zagnieżdżone i podagregacje”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Elasticsearch & Full Text Search Systems?
Tak. Każda lekcja Elasticsearch & Full Text Search Systems zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Agregacje metryczne
- Agregacje kubełkowe
- Agregacje potokowe
- Agregacje zagnieżdżone i podagregacje