liveBook · Manning

Sibling aggregation

As explained in Elasticsearch in Action, Second Edition

A sibling pipeline aggregation is a pipeline aggregation declared at the same level as the aggregation whose buckets it processes.

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

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains same-level placement, newly produced output, and buckets_path traversal, then demonstrates max_bucket and min_bucket.
Examples
Uses daily coffee sales with date_histogram, sum, max_bucket, and min_bucket to identify highest and lowest cappuccino-sales days.
What it assumes
Assumes familiarity with metric and bucket aggregations, while demonstrating their relationship through date_histogram and sum.
What you can do after
Builds REST aggregation queries that compare or summarize metrics across buckets using max_bucket, min_bucket, and buckets_path.
How it connects
Connects sibling aggregations with parent aggregations, pipeline aggregations, bucket aggregations, metric aggregations, and buckets_path.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.