Type to search.

Bucket script aggregation context

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

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.

JSON
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"
            }
          }
        }
      }
    }
  }
}