elasticsearch
Loading

Bucket script aggregation context

Serverless Stack

Use a Painless script in an bucket_script pipeline aggregation to calculate a value as a result in a bucket.

params (Map, read-only)
User-defined parameters passed in as part of the query. The parameters include values defined as part of the buckets_path.
numeric
The calculated value as the result.

The standard Painless API is available.

To run the example, first install the eCommerce sample data.

The following request is useful for identifying high-value markets by comparing average order values across countries. It groups orders by country and calculates metrics for each country, including total_revenue and order_count, then uses a bucket script to compute revenue_per_order for performance analysis.

GET kibana_sample_data_ecommerce/_search
{
  "size": 0,
  "aggs": {
    "countries": {
      "terms": {
        "field": "geoip.country_iso_code"
      },
      "aggs": {
        "total_revenue": {
          "sum": {
            "field": "taxful_total_price"
          }
        },
        "order_count": {
          "value_count": {
            "field": "order_id"
          }
        },
        "revenue_per_order": {
          "bucket_script": {
            "buckets_path": {
              "revenue": "total_revenue",
              "orders": "order_count"
            },
            "script": {
              "source": "params.revenue / params.orders"
            }
          }
        }
      }
    }
  }
}