scripted-metric-aggregation.asciidoc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. [[search-aggregations-metrics-scripted-metric-aggregation]]
  2. === Scripted Metric Aggregation
  3. experimental[]
  4. A metric aggregation that executes using scripts to provide a metric output.
  5. Example:
  6. [source,js]
  7. --------------------------------------------------
  8. {
  9. "query" : {
  10. "match_all" : {}
  11. },
  12. "aggs": {
  13. "profit": {
  14. "scripted_metric": {
  15. "init_script" : "_agg['transactions'] = []",
  16. "map_script" : "if (doc['type'].value == \"sale\") { _agg.transactions.add(doc['amount'].value) } else { _agg.transactions.add(-1 * doc['amount'].value) }", <1>
  17. "combine_script" : "profit = 0; for (t in _agg.transactions) { profit += t }; return profit",
  18. "reduce_script" : "profit = 0; for (a in _aggs) { profit += a }; return profit"
  19. }
  20. }
  21. }
  22. }
  23. --------------------------------------------------
  24. <1> `map_script` is the only required parameter
  25. The above aggregation demonstrates how one would use the script aggregation compute the total profit from sale and cost transactions.
  26. The response for the above aggregation:
  27. [source,js]
  28. --------------------------------------------------
  29. {
  30. ...
  31. "aggregations": {
  32. "profit": {
  33. "value": 170
  34. }
  35. }
  36. }
  37. --------------------------------------------------
  38. The above example can also be specified using file scripts as follows:
  39. [source,js]
  40. --------------------------------------------------
  41. {
  42. "query" : {
  43. "match_all" : {}
  44. },
  45. "aggs": {
  46. "profit": {
  47. "scripted_metric": {
  48. "init_script" : {
  49. "file": "my_init_script"
  50. },
  51. "map_script" : {
  52. "file": "my_map_script"
  53. },
  54. "combine_script" : {
  55. "file": "my_combine_script"
  56. },
  57. "params": {
  58. "field": "amount" <1>
  59. },
  60. "reduce_script" : {
  61. "file": "my_reduce_script"
  62. },
  63. }
  64. }
  65. }
  66. }
  67. --------------------------------------------------
  68. <1> script parameters for init, map and combine scripts must be specified in a global `params` object so that it can be share between the scripts
  69. For more details on specifying scripts see <<modules-scripting, script documentation>>.
  70. ==== Scope of scripts
  71. The scripted metric aggregation uses scripts at 4 stages of its execution:
  72. init_script:: Executed prior to any collection of documents. Allows the aggregation to set up any initial state.
  73. +
  74. In the above example, the `init_script` creates an array `transactions` in the `_agg` object.
  75. map_script:: Executed once per document collected. This is the only required script. If no combine_script is specified, the resulting state
  76. needs to be stored in an object named `_agg`.
  77. +
  78. In the above example, the `map_script` checks the value of the type field. If the value if 'sale' the value of the amount field
  79. 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
  80. to transactions.
  81. combine_script:: Executed once on each shard after document collection is complete. Allows the aggregation to consolidate the state returned from
  82. each shard. If a combine_script is not provided the combine phase will return the aggregation variable.
  83. +
  84. In the above example, the `combine_script` iterates through all the stored transactions, summing the values in the `profit` variable
  85. and finally returns `profit`.
  86. reduce_script:: Executed once on the coordinating node after all shards have returned their results. The script is provided with access to a
  87. variable `_aggs` which is an array of the result of the combine_script on each shard. If a reduce_script is not provided
  88. the reduce phase will return the `_aggs` variable.
  89. +
  90. In the above example, the `reduce_script` iterates through the `profit` returned by each shard summing the values before returning the
  91. final combined profit which will be returned in the response of the aggregation.
  92. ==== Worked Example
  93. Imagine a situation where you index the following documents into and index with 2 shards:
  94. [source,js]
  95. --------------------------------------------------
  96. $ curl -XPUT 'http://localhost:9200/transactions/stock/1' -d '
  97. {
  98. "type": "sale",
  99. "amount": 80
  100. }
  101. '
  102. $ curl -XPUT 'http://localhost:9200/transactions/stock/2' -d '
  103. {
  104. "type": "cost",
  105. "amount": 10
  106. }
  107. '
  108. $ curl -XPUT 'http://localhost:9200/transactions/stock/3' -d '
  109. {
  110. "type": "cost",
  111. "amount": 30
  112. }
  113. '
  114. $ curl -XPUT 'http://localhost:9200/transactions/stock/4' -d '
  115. {
  116. "type": "sale",
  117. "amount": 130
  118. }
  119. '
  120. --------------------------------------------------
  121. Lets say that documents 1 and 3 end up on shard A and documents 2 and 4 end up on shard B. The following is a breakdown of what the aggregation result is
  122. at each stage of the example above.
  123. ===== Before init_script
  124. No params object was specified so the default params object is used:
  125. [source,js]
  126. --------------------------------------------------
  127. "params" : {
  128. "_agg" : {}
  129. }
  130. --------------------------------------------------
  131. ===== After init_script
  132. This is run once on each shard before any document collection is performed, and so we will have a copy on each shard:
  133. Shard A::
  134. +
  135. [source,js]
  136. --------------------------------------------------
  137. "params" : {
  138. "_agg" : {
  139. "transactions" : []
  140. }
  141. }
  142. --------------------------------------------------
  143. Shard B::
  144. +
  145. [source,js]
  146. --------------------------------------------------
  147. "params" : {
  148. "_agg" : {
  149. "transactions" : []
  150. }
  151. }
  152. --------------------------------------------------
  153. ===== After map_script
  154. Each shard collects its documents and runs the map_script on each document that is collected:
  155. Shard A::
  156. +
  157. [source,js]
  158. --------------------------------------------------
  159. "params" : {
  160. "_agg" : {
  161. "transactions" : [ 80, -30 ]
  162. }
  163. }
  164. --------------------------------------------------
  165. Shard B::
  166. +
  167. [source,js]
  168. --------------------------------------------------
  169. "params" : {
  170. "_agg" : {
  171. "transactions" : [ -10, 130 ]
  172. }
  173. }
  174. --------------------------------------------------
  175. ===== After combine_script
  176. The combine_script is executed on each shard after document collection is complete and reduces all the transactions down to a single profit figure for each
  177. shard (by summing the values in the transactions array) which is passed back to the coordinating node:
  178. Shard A:: 50
  179. Shard B:: 120
  180. ===== After reduce_script
  181. The reduce_script receives an `_aggs` array containing the result of the combine script for each shard:
  182. [source,js]
  183. --------------------------------------------------
  184. "_aggs" : [
  185. 50,
  186. 120
  187. ]
  188. --------------------------------------------------
  189. It reduces the responses for the shards down to a final overall profit figure (by summing the values) and returns this as the result of the aggregation to
  190. produce the response:
  191. [source,js]
  192. --------------------------------------------------
  193. {
  194. ...
  195. "aggregations": {
  196. "profit": {
  197. "value": 170
  198. }
  199. }
  200. }
  201. --------------------------------------------------
  202. ==== Other Parameters
  203. [horizontal]
  204. params:: Optional. An object whose contents will be passed as variables to the `init_script`, `map_script` and `combine_script`. This can be
  205. useful to allow the user to control the behavior of the aggregation and for storing state between the scripts. If this is not specified,
  206. the default is the equivalent of providing:
  207. +
  208. [source,js]
  209. --------------------------------------------------
  210. "params" : {
  211. "_agg" : {}
  212. }
  213. --------------------------------------------------
  214. reduce_params:: Optional. An object whose contents will be passed as variables to the `reduce_script`. This can be useful to allow the user to control
  215. the behavior of the reduce phase. If this is not specified the variable will be undefined in the reduce_script execution.