@@ -15,10 +15,10 @@ POST ledger/_search?size=0
1515 "aggs": {
1616 "profit": {
1717 "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"
2222 }
2323 }
2424 }
@@ -67,8 +67,7 @@ POST ledger/_search?size=0
6767 "id": "my_combine_script"
6868 },
6969 "params": {
70- "field": "amount", <1>
71- "_agg": {} <2>
70+ "field": "amount" <1>
7271 },
7372 "reduce_script" : {
7473 "id": "my_reduce_script"
@@ -82,8 +81,7 @@ POST ledger/_search?size=0
8281// TEST[setup:ledger,stored_scripted_metric_script]
8382
8483<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.
8785
8886////
8987Verify this response as well but in a hidden block.
@@ -108,7 +106,7 @@ For more details on specifying scripts see <<modules-scripting, script documenta
108106
109107==== Allowed return types
110108
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:
112110
113111* primitive types
114112* String
@@ -121,10 +119,10 @@ The scripted metric aggregation uses scripts at 4 stages of its execution:
121119
122120init_script:: Executed prior to any collection of documents. Allows the aggregation to set up any initial state.
123121+
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.
125123
126124map_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 .
128126+
129127In the above example, the `map_script` checks the value of the type field. If the value is 'sale' the value of the amount field
130128is 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
137135and finally returns `profit`.
138136
139137reduce_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.
142140+
143141In the above example, the `reduce_script` iterates through the `profit` returned by each shard summing the values before returning the
144142final combined profit which will be returned in the response of the aggregation.
@@ -166,13 +164,11 @@ at each stage of the example above.
166164
167165===== Before init_script
168166
169- No params object was specified so the default params object is used:
167+ `state` is initialized as a new empty object.
170168
171169[source,js]
172170--------------------------------------------------
173- "params" : {
174- "_agg" : {}
175- }
171+ "state" : {}
176172--------------------------------------------------
177173// NOTCONSOLE
178174
@@ -184,10 +180,8 @@ Shard A::
184180+
185181[source,js]
186182--------------------------------------------------
187- "params" : {
188- "_agg" : {
189- "transactions" : []
190- }
183+ "state" : {
184+ "transactions" : []
191185}
192186--------------------------------------------------
193187// NOTCONSOLE
@@ -196,10 +190,8 @@ Shard B::
196190+
197191[source,js]
198192--------------------------------------------------
199- "params" : {
200- "_agg" : {
201- "transactions" : []
202- }
193+ "state" : {
194+ "transactions" : []
203195}
204196--------------------------------------------------
205197// NOTCONSOLE
@@ -212,10 +204,8 @@ Shard A::
212204+
213205[source,js]
214206--------------------------------------------------
215- "params" : {
216- "_agg" : {
217- "transactions" : [ 80, -30 ]
218- }
207+ "state" : {
208+ "transactions" : [ 80, -30 ]
219209}
220210--------------------------------------------------
221211// NOTCONSOLE
@@ -224,10 +214,8 @@ Shard B::
224214+
225215[source,js]
226216--------------------------------------------------
227- "params" : {
228- "_agg" : {
229- "transactions" : [ -10, 130 ]
230- }
217+ "state" : {
218+ "transactions" : [ -10, 130 ]
231219}
232220--------------------------------------------------
233221// NOTCONSOLE
@@ -242,11 +230,11 @@ Shard B:: 120
242230
243231===== After reduce_script
244232
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:
246234
247235[source,js]
248236--------------------------------------------------
249- "_aggs " : [
237+ "states " : [
250238 50,
251239 120
252240]
@@ -279,14 +267,12 @@ params:: Optional. An object whose contents will be passed as variable
279267+
280268[source,js]
281269--------------------------------------------------
282- "params" : {
283- "_agg" : {}
284- }
270+ "params" : {}
285271--------------------------------------------------
286272// NOTCONSOLE
287273
288274==== Empty Buckets
289275
290276If 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.
292278`reduce_script`'s should therefore expect and deal with `null` responses from shards.
0 commit comments