Back to Elasticsearch

Bucket selector aggregation context [painless-bucket-selector-agg-context]

docs/reference/scripting-languages/painless/painless-bucket-selector-agg-context.md

9.4.01.8 KB
Original Source

Bucket selector aggregation context [painless-bucket-selector-agg-context]

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

Variables [_variables_2]

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.

Return [_return_2]

boolean : True if the bucket should be retained, false if the bucket should be filtered out.

API [_api_2]

The standard Painless API is available.

Example [_example_2]

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.

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