Skip to content

Commit 065264d

Browse files
authored
[Docs] Add painless context details for bucket_script (#35142)
Fleshes out the details of the bucket_script context, adds an example and corresponding doc test
1 parent 432c518 commit 065264d

5 files changed

+118
-23
lines changed

docs/build.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,3 +1128,33 @@ buildRestTests.setups['remote_cluster_and_leader_index'] = buildRestTests.setups
11281128
index.number_of_shards: 1
11291129
index.soft_deletes.enabled: true
11301130
'''
1131+
1132+
buildRestTests.setups['seats'] = '''
1133+
- do:
1134+
indices.create:
1135+
index: seats
1136+
body:
1137+
settings:
1138+
number_of_shards: 1
1139+
number_of_replicas: 0
1140+
mappings:
1141+
_doc:
1142+
properties:
1143+
theatre:
1144+
type: keyword
1145+
cost:
1146+
type: long
1147+
- do:
1148+
bulk:
1149+
index: seats
1150+
type: _doc
1151+
refresh: true
1152+
body: |
1153+
{"index":{}}
1154+
{"theatre": "Skyline", "cost": 1}
1155+
{"index":{}}
1156+
{"theatre": "Graye", "cost": 5}
1157+
{"index":{}}
1158+
{"theatre": "Graye", "cost": 8}
1159+
{"index":{}}
1160+
{"theatre": "Skyline", "cost": 10}'''

docs/painless/painless-contexts.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ specialized code may define new ways to use a Painless script.
4444
| {ref}/search-aggregations-metrics-scripted-metric-aggregation.html[Elasticsearch Documentation]
4545
| Metric aggregation reduce | <<painless-metric-agg-reduce-context, Painless Documentation>>
4646
| {ref}/search-aggregations-metrics-scripted-metric-aggregation.html[Elasticsearch Documentation]
47-
| Bucket aggregation | <<painless-bucket-agg-context, Painless Documentation>>
47+
| Bucket script aggregation | <<painless-bucket-script-agg-context, Painless Documentation>>
4848
| {ref}/search-aggregations-pipeline-bucket-script-aggregation.html[Elasticsearch Documentation]
4949
| Watcher condition | <<painless-watcher-condition-context, Painless Documentation>>
5050
| {xpack-ref}/condition-script.html[Elasticsearch Documentation]

docs/painless/painless-contexts/index.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ include::painless-metric-agg-combine-context.asciidoc[]
2828

2929
include::painless-metric-agg-reduce-context.asciidoc[]
3030

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

3333
include::painless-analysis-predicate-context.asciidoc[]
3434

docs/painless/painless-contexts/painless-bucket-agg-context.asciidoc

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[[painless-bucket-script-agg-context]]
2+
=== Bucket script aggregation context
3+
4+
Use a Painless script in an
5+
{ref}/search-aggregations-pipeline-bucket-script-aggregation.html[`bucket_script` pipeline aggregation]
6+
to calculate a value as a result in a bucket.
7+
8+
==== Variables
9+
10+
`params` (`Map`, read-only)::
11+
User-defined parameters passed in as part of the query. The parameters
12+
include values defined as part of the `buckets_path`.
13+
14+
==== Return
15+
16+
numeric::
17+
The calculated value as the result.
18+
19+
==== API
20+
21+
The standard <<painless-api-reference, Painless API>> is available.
22+
23+
==== Example
24+
25+
To run this example, first follow the steps in <<painless-context-examples, context examples>>.
26+
27+
The painless context in a `bucket_script` aggregation provides a `params` map. This map contains both
28+
user-specified custom values, as well as the values from other aggregations specified in the `buckets_path`
29+
property.
30+
31+
This example takes the values from a min and max aggregation, calculates the difference,
32+
and adds the user-specified base_cost to the result:
33+
34+
[source,Painless]
35+
--------------------------------------------------
36+
(params.max - params.min) + params.base_cost
37+
--------------------------------------------------
38+
39+
Note that the values are extracted from the `params` map. In context, the aggregation looks like this:
40+
41+
[source,js]
42+
--------------------------------------------------
43+
GET /seats/_search
44+
{
45+
"size": 0,
46+
"aggs": {
47+
"theatres": {
48+
"terms": {
49+
"field": "theatre",
50+
"size": 10
51+
},
52+
"aggs": {
53+
"min_cost": {
54+
"min": {
55+
"field": "cost"
56+
}
57+
},
58+
"max_cost": {
59+
"max": {
60+
"field": "cost"
61+
}
62+
},
63+
"spread_plus_base": {
64+
"bucket_script": {
65+
"buckets_path": { <1>
66+
"min": "min_cost",
67+
"max": "max_cost"
68+
},
69+
"script": {
70+
"params": {
71+
"base_cost": 5 <2>
72+
},
73+
"source": "(params.max - params.min) + params.base_cost"
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
80+
}
81+
--------------------------------------------------
82+
// CONSOLE
83+
// TEST[setup:seats]
84+
<1> The `buckets_path` points to two aggregations (`min_cost`, `max_cost`) and adds `min`/`max` variables
85+
to the `params` map
86+
<2> The user-specified `base_cost` is also added to the script's `params` map

0 commit comments

Comments
 (0)