@@ -15,10 +15,10 @@ POST ledger/_search?size=0
15
15
"aggs": {
16
16
"profit": {
17
17
"scripted_metric": {
18
- "init_script" : "params._agg .transactions = []",
19
- "map_script" : "params._agg .transactions.add(doc.type.value == 'sale' ? doc.amount.value : -1 * doc.amount.value)", <1>
20
- "combine_script" : "double profit = 0; for (t in params._agg .transactions) { profit += t } return profit",
21
- "reduce_script" : "double profit = 0; for (a in params._aggs ) { profit += a } return profit"
18
+ "init_script" : "state .transactions = []",
19
+ "map_script" : "state .transactions.add(doc.type.value == 'sale' ? doc.amount.value : -1 * doc.amount.value)", <1>
20
+ "combine_script" : "double profit = 0; for (t in state .transactions) { profit += t } return profit",
21
+ "reduce_script" : "double profit = 0; for (a in states ) { profit += a } return profit"
22
22
}
23
23
}
24
24
}
@@ -67,8 +67,7 @@ POST ledger/_search?size=0
67
67
"id": "my_combine_script"
68
68
},
69
69
"params": {
70
- "field": "amount", <1>
71
- "_agg": {} <2>
70
+ "field": "amount" <1>
72
71
},
73
72
"reduce_script" : {
74
73
"id": "my_reduce_script"
@@ -82,8 +81,7 @@ POST ledger/_search?size=0
82
81
// TEST[setup:ledger,stored_scripted_metric_script]
83
82
84
83
<1> script parameters for `init`, `map` and `combine` scripts must be specified
85
- in a global `params` object so that it can be share between the scripts.
86
- <2> if you specify script parameters then you must specify `"_agg": {}`.
84
+ in a global `params` object so that it can be shared between the scripts.
87
85
88
86
////
89
87
Verify this response as well but in a hidden block.
@@ -108,7 +106,7 @@ For more details on specifying scripts see <<modules-scripting, script documenta
108
106
109
107
==== Allowed return types
110
108
111
- Whilst any valid script object can be used within a single script, the scripts must return or store in the `_agg ` object only the following types:
109
+ Whilst any valid script object can be used within a single script, the scripts must return or store in the `state ` object only the following types:
112
110
113
111
* primitive types
114
112
* String
@@ -121,10 +119,10 @@ The scripted metric aggregation uses scripts at 4 stages of its execution:
121
119
122
120
init_script:: Executed prior to any collection of documents. Allows the aggregation to set up any initial state.
123
121
+
124
- In the above example, the `init_script` creates an array `transactions` in the `_agg ` object.
122
+ In the above example, the `init_script` creates an array `transactions` in the `state ` object.
125
123
126
124
map_script:: Executed once per document collected. This is the only required script. If no combine_script is specified, the resulting state
127
- needs to be stored in an object named `_agg` .
125
+ needs to be stored in the `state` object .
128
126
+
129
127
In the above example, the `map_script` checks the value of the type field. If the value is 'sale' the value of the amount field
130
128
is added to the transactions array. If the value of the type field is not 'sale' the negated value of the amount field is added
@@ -137,8 +135,8 @@ In the above example, the `combine_script` iterates through all the stored trans
137
135
and finally returns `profit`.
138
136
139
137
reduce_script:: Executed once on the coordinating node after all shards have returned their results. The script is provided with access to a
140
- variable `_aggs ` which is an array of the result of the combine_script on each shard. If a reduce_script is not provided
141
- the reduce phase will return the `_aggs ` variable.
138
+ variable `states ` which is an array of the result of the combine_script on each shard. If a reduce_script is not provided
139
+ the reduce phase will return the `states ` variable.
142
140
+
143
141
In the above example, the `reduce_script` iterates through the `profit` returned by each shard summing the values before returning the
144
142
final combined profit which will be returned in the response of the aggregation.
@@ -166,13 +164,11 @@ at each stage of the example above.
166
164
167
165
===== Before init_script
168
166
169
- No params object was specified so the default params object is used:
167
+ `state` is initialized as a new empty object.
170
168
171
169
[source,js]
172
170
--------------------------------------------------
173
- "params" : {
174
- "_agg" : {}
175
- }
171
+ "state" : {}
176
172
--------------------------------------------------
177
173
// NOTCONSOLE
178
174
@@ -184,10 +180,8 @@ Shard A::
184
180
+
185
181
[source,js]
186
182
--------------------------------------------------
187
- "params" : {
188
- "_agg" : {
189
- "transactions" : []
190
- }
183
+ "state" : {
184
+ "transactions" : []
191
185
}
192
186
--------------------------------------------------
193
187
// NOTCONSOLE
@@ -196,10 +190,8 @@ Shard B::
196
190
+
197
191
[source,js]
198
192
--------------------------------------------------
199
- "params" : {
200
- "_agg" : {
201
- "transactions" : []
202
- }
193
+ "state" : {
194
+ "transactions" : []
203
195
}
204
196
--------------------------------------------------
205
197
// NOTCONSOLE
@@ -212,10 +204,8 @@ Shard A::
212
204
+
213
205
[source,js]
214
206
--------------------------------------------------
215
- "params" : {
216
- "_agg" : {
217
- "transactions" : [ 80, -30 ]
218
- }
207
+ "state" : {
208
+ "transactions" : [ 80, -30 ]
219
209
}
220
210
--------------------------------------------------
221
211
// NOTCONSOLE
@@ -224,10 +214,8 @@ Shard B::
224
214
+
225
215
[source,js]
226
216
--------------------------------------------------
227
- "params" : {
228
- "_agg" : {
229
- "transactions" : [ -10, 130 ]
230
- }
217
+ "state" : {
218
+ "transactions" : [ -10, 130 ]
231
219
}
232
220
--------------------------------------------------
233
221
// NOTCONSOLE
@@ -242,11 +230,11 @@ Shard B:: 120
242
230
243
231
===== After reduce_script
244
232
245
- The reduce_script receives an `_aggs ` array containing the result of the combine script for each shard:
233
+ The reduce_script receives a `states ` array containing the result of the combine script for each shard:
246
234
247
235
[source,js]
248
236
--------------------------------------------------
249
- "_aggs " : [
237
+ "states " : [
250
238
50,
251
239
120
252
240
]
@@ -279,14 +267,12 @@ params:: Optional. An object whose contents will be passed as variable
279
267
+
280
268
[source,js]
281
269
--------------------------------------------------
282
- "params" : {
283
- "_agg" : {}
284
- }
270
+ "params" : {}
285
271
--------------------------------------------------
286
272
// NOTCONSOLE
287
273
288
274
==== Empty Buckets
289
275
290
276
If a parent bucket of the scripted metric aggregation does not collect any documents an empty aggregation response will be returned from the
291
- shard with a `null` value. In this case the `reduce_script`'s `_aggs ` variable will contain `null` as a response from that shard.
277
+ shard with a `null` value. In this case the `reduce_script`'s `states ` variable will contain `null` as a response from that shard.
292
278
`reduce_script`'s should therefore expect and deal with `null` responses from shards.
0 commit comments