liveBook · Manning

Parent pipeline aggregation

As explained in Elasticsearch in Action, Second Edition

A parent pipeline aggregation operates on child aggregation output and adds results to existing buckets.

A parent pipeline aggregation consumes the output of a child metric or bucket aggregation and adds a new aggregation or buckets inside the existing parent buckets. This differs from a sibling pipeline aggregation, which creates its result at the same level as the source aggregation. section 13.4.1 section 13.5.1

Use the structure to identify where the calculation belongs:

  • Place a bucket aggregation such as date_histogram at the parent level. section 13.5.5
  • Define a child metric aggregation, such as a sum of cappuccino sales. section 13.4.1
  • Add the pipeline aggregation beside that child metric and identify its input with buckets_path. section 13.5.3
  • Read the calculated value from each existing parent bucket. section 13.5.5

Cumulative cappuccino sales by 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" } }, "total_cappuccinos": { "cumulative_sum": { "buckets_path": "cappuccino_sales" } } } } } } Here, cappuccino_sales supplies the daily input and total_cappuccinos accumulates it within sales_by_coffee. With daily values of 23 and 40, the returned cumulative values are 23 and 63. section 13.5.5

Listing 13.22 Cumulative sales (sum) of cappuccinos sold daily
GET coffee_sales/_search
{
  "size": 0,
  "aggs": {
    "sales_by_coffee": {
      "date_histogram": {
        "field": "date",
        "calendar_interval": "1d"
      },
      "aggs": {
        "cappuccino_sales": {
          "sum": {
            "field": "sales.cappuccino"
          }
        },
        "total_cappuccinos": {
          "cumulative_sum": {                   #1
            "buckets_path": "cappuccino_sales"
          }
        }
      }
    }
  }
}
#1 Parent aggregation that calculates the cumulative total of cappuccino sales

Inspect the complete REST aggregation query for a runnable parent pipeline example. section 13.5.5

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains parent-child placement, buckets_path input references, and a cumulative_sum example, while deferring exhaustive pipeline types.
Examples
Uses coffee-sales documents grouped by day to accumulate cappuccino counts from 23 and 40 into 23 and 63.
What it assumes
Assumes basic bucket and metric aggregations, while establishing parent, sibling, and buckets_path concepts for pipeline work.
What you can do after
Build parent pipeline aggregations with REST JSON, placing cumulative_sum beside its source metric and referencing that metric through buckets_path.
How it connects
Connects pipeline aggregations with child aggregations, bucket aggregations, date histograms, cumulative sums, and sibling pipeline aggregations.

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.