elasticsearch
Loading

Bucket selector aggregation context

Serverless Stack

Use a Painless script in an bucket_selector aggregation to determine if a bucket should be retained or filtered out.

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.
boolean
True if the bucket should be retained, false if the bucket should be filtered out.

The standard Painless API is available.

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

The following request filters out low-performing manufacturers and focuses only on brands with significant sales volume. The query groups orders by manufacturer, counts total orders for each brand, then uses a bucket selector to show only manufacturers with 50 or more orders.

GET kibana_sample_data_ecommerce/_search
{
  "size": 0,
  "aggs": {
    "manufacturers": {
      "terms": {
        "field": "manufacturer.keyword"
      },
      "aggs": {
        "total_orders": {
          "value_count": {
            "field": "order_id"
          }
        },
        "high_volume_filter": {
          "bucket_selector": {
            "buckets_path": {
              "order_count": "total_orders"
            },
            "script": {
              "source": "params.order_count >= 50"
            }
          }
        }
      }
    }
  }
}