|
| 1 | + |
| 2 | +[[painless-bucket-selector-agg-context]] |
| 3 | +=== Bucket selector aggregation context |
| 4 | + |
| 5 | +Use a Painless script in an |
| 6 | +{ref}/search-aggregations-pipeline-bucket-selector-aggregation.html[`bucket_selector` aggregation] |
| 7 | +to determine if a bucket should be retained or filtered out. |
| 8 | + |
| 9 | +==== Variables |
| 10 | + |
| 11 | +`params` (`Map`, read-only):: |
| 12 | + User-defined parameters passed in as part of the query. The parameters |
| 13 | + include values defined as part of the `buckets_path`. |
| 14 | + |
| 15 | +==== Return |
| 16 | + |
| 17 | +boolean:: |
| 18 | + True if the the bucket should be retained, false if the bucket should be filtered out. |
| 19 | + |
| 20 | +==== API |
| 21 | + |
| 22 | + |
| 23 | +To run this example, first follow the steps in <<painless-context-examples, context examples>>. |
| 24 | + |
| 25 | +The painless context in a `bucket_selector` aggregation provides a `params` map. This map contains both |
| 26 | +user-specified custom values, as well as the values from other aggregations specified in the `buckets_path` |
| 27 | +property. |
| 28 | + |
| 29 | +Unlike some other aggregation contexts, the `bucket_selector` context must return a boolean `true` or `false`. |
| 30 | + |
| 31 | +This example finds the max of each bucket, adds a user-specified base_cost, and retains all of the |
| 32 | +buckets that are greater than `10`. |
| 33 | + |
| 34 | +[source,Painless] |
| 35 | +-------------------------------------------------- |
| 36 | +params.max + params.base_cost > 10 |
| 37 | +-------------------------------------------------- |
| 38 | + |
| 39 | +Note that the values are extracted from the `params` map. The script is in the form of an expression |
| 40 | +that returns `true` or `false`. In context, the aggregation looks like this: |
| 41 | + |
| 42 | +[source,js] |
| 43 | +-------------------------------------------------- |
| 44 | +GET /seats/_search |
| 45 | +{ |
| 46 | + "size": 0, |
| 47 | + "aggs": { |
| 48 | + "theatres": { |
| 49 | + "terms": { |
| 50 | + "field": "theatre", |
| 51 | + "size": 10 |
| 52 | + }, |
| 53 | + "aggs": { |
| 54 | + "max_cost": { |
| 55 | + "max": { |
| 56 | + "field": "cost" |
| 57 | + } |
| 58 | + }, |
| 59 | + "filtering_agg": { |
| 60 | + "bucket_selector": { |
| 61 | + "buckets_path": { <1> |
| 62 | + "max": "max_cost" |
| 63 | + }, |
| 64 | + "script": { |
| 65 | + "params": { |
| 66 | + "base_cost": 5 <2> |
| 67 | + }, |
| 68 | + "source": "params.max + params.base_cost > 10" |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +-------------------------------------------------- |
| 77 | +// CONSOLE |
| 78 | +// TEST[setup:seats] |
| 79 | +<1> The `buckets_path` points to the max aggregations (`max_cost`) and adds `max` variables |
| 80 | +to the `params` map |
| 81 | +<2> The user-specified `base_cost` is also added to the `params` map |
0 commit comments