To find the day with the highest cappuccino sales, first group sales by day and calculate daily totals. A sibling aggregation then reads those results and emits a separate result at the same aggregation level. section 13.4.2 section 13.5.6
A sibling aggregation has three defining traits:
- It is declared beside the aggregation whose buckets or metrics it processes, rather than inside that aggregation. section 13.4.2
- It produces a new aggregation or new buckets at the surrounding, root aggregation level. section 13.4.2
- A sibling pipeline aggregation identifies its input with buckets_path, which can traverse an aggregation and its sub-aggregation using >. section 13.5.3
Find the best sales day
GET coffee_sales/_search { "size": 0, "aggs": { "sales_by_coffee": { "date_histogram": { "field": "date", "calendar_interval": "1d" }, "aggs": { "cappuccino_sales": { "sum": { "field": "sales.cappuccino" } } } }, "highest_cappuccino_sales_bucket": { "max_bucket": { "buckets_path": "sales_by_coffee>cappuccino_sales" } } } } Here, sales_by_coffee creates daily buckets and cappuccino_sales calculates each bucket’s total. highest_cappuccino_sales_bucket is its sibling: it reads those nested totals and returns the maximum value and matching date. section 13.5.6
Listing 13.23 Pipeline aggregation to find to sales of cappuccinos
GET coffee_sales/_search { "size": 0, "aggs": { "sales_by_coffee": { "date_histogram": { "field": "date", "calendar_interval": "1d" }, "aggs": { "cappuccino_sales": { "sum": { "field": "sales.cappuccino" } } } }, "highest_cappuccino_sales_bucket":{ "max_bucket": { "buckets_path": "sales_by_coffee>cappuccino_sales" } } } }
Inspect the complete REST query showing a date histogram, a nested sum metric, and a same-level max_bucket pipeline aggregation. section 13.5.6
Use min_bucket instead of max_bucket to find the lowest-valued day. More generally, sibling pipeline aggregations include average, maximum, minimum, percentile, statistics, and sum operations over sibling metrics. section 13.5.4 section 13.5.6
