Skip to content

Commit 5c8bd2f

Browse files
committed
[Docs] Add painless context details for bucket_selector (#35162)
Adds docs for the bucket_selector context, an example and corresponding doc test
1 parent c973cef commit 5c8bd2f

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

docs/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,4 +1153,4 @@ buildRestTests.setups['seats'] = '''
11531153
{"index":{}}
11541154
{"theatre": "Graye", "cost": 8}
11551155
{"index":{}}
1156-
{"theatre": "Skyline", "cost": 10}'''
1156+
{"theatre": "Skyline", "cost": 10}'''

docs/painless/painless-contexts.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ specialized code may define new ways to use a Painless script.
4646
| {ref}/search-aggregations-metrics-scripted-metric-aggregation.html[Elasticsearch Documentation]
4747
| Bucket script aggregation | <<painless-bucket-script-agg-context, Painless Documentation>>
4848
| {ref}/search-aggregations-pipeline-bucket-script-aggregation.html[Elasticsearch Documentation]
49+
| Bucket selector aggregation | <<painless-bucket-selector-agg-context, Painless Documentation>>
50+
| {ref}/search-aggregations-pipeline-bucket-selector-aggregation.html[Elasticsearch Documentation]
4951
| Watcher condition | <<painless-watcher-condition-context, Painless Documentation>>
5052
| {xpack-ref}/condition-script.html[Elasticsearch Documentation]
5153
| Watcher transform | <<painless-watcher-transform-context, Painless Documentation>>

docs/painless/painless-contexts/index.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ include::painless-metric-agg-reduce-context.asciidoc[]
3030

3131
include::painless-bucket-script-agg-context.asciidoc[]
3232

33+
include::painless-bucket-selector-agg-context.asciidoc[]
34+
3335
include::painless-analysis-predicate-context.asciidoc[]
3436

3537
include::painless-watcher-condition-context.asciidoc[]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)