Nested集約とサブ集約
バケット内にメトリクスをネストして集約を組み合わせ、多層の分析を構築し、nested集約でネストされたドキュメント構造をたどる方法を学びます。
「Nested集約とサブ集約」はCoddyKit上の無料Elasticsearch & Full Text Search Systemsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはElasticsearch & Full Text Search Systems学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Elasticsearch & Full Text Search Systemsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
AI チューターと学ぶ Elasticsearch & Full Text Search Systems — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 48
よくある質問
「Nested集約とサブ集約」レッスンは無料ですか?
はい。「Nested集約とサブ集約」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Elasticsearch & Full Text Search Systemsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Elasticsearch & Full Text Search Systemsコースには全4レッスンが含まれています。
「Nested集約とサブ集約」で何を学びますか?
バケット内にメトリクスをネストして集約を組み合わせ、多層の分析を構築し、nested集約でネストされたドキュメント構造をたどる方法を学びます。 ブラウザで直接実行するハンズオンコードでElasticsearch & Full Text Search Systemsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Elasticsearch & Full Text Search Systemsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのElasticsearch & Full Text Search Systemsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Nested集約とサブ集約」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このElasticsearch & Full Text Search Systemsレッスンでコードを書いて実行できますか?
はい。すべてのElasticsearch & Full Text Search Systemsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。