scripted-metric-aggregation.asciidoc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. ==== Scope of scripts
  39. The scripted metric aggregation uses scripts at 4 stages of its execution:
  40. init_script:: Executed prior to any collection of documents. Allows the aggregation to set up any initial state.
  41. +
  42. In the above example, the `init_script` creates an array `transactions` in the `_agg` object.
  43. map_script:: Executed once per document collected. This is the only required script. If no combine_script is specified, the resulting state
  44. needs to be stored in an object named `_agg`.
  45. +
  46. 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
  47. 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
  48. to transactions.
  49. combine_script:: Executed once on each shard after document collection is complete. Allows the aggregation to consolidate the state returned from
  50. each shard. If a combine_script is not provided the combine phase will return the aggregation variable.
  51. +
  52. In the above example, the `combine_script` iterates through all the stored transactions, summing the values in the `profit` variable
  53. and finally returns `profit`.
  54. reduce_script:: Executed once on the coordinating node after all shards have returned their results. The script is provided with access to a
  55. variable `_aggs` which is an array of the result of the combine_script on each shard. If a reduce_script is not provided
  56. the reduce phase will return the `_aggs` variable.
  57. +
  58. In the above example, the `reduce_script` iterates through the `profit` returned by each shard summing the values before returning the
  59. final combined profit which will be returned in the response of the aggregation.
  60. ==== Worked Example
  61. Imagine a situation where you index the following documents into and index with 2 shards:
  62. [source,js]
  63. --------------------------------------------------
  64. $ curl -XPUT 'http://localhost:9200/transactions/stock/1' -d '
  65. {
  66. "type": "sale",
  67. "amount": 80
  68. }
  69. '
  70. $ curl -XPUT 'http://localhost:9200/transactions/stock/2' -d '
  71. {
  72. "type": "cost",
  73. "amount": 10
  74. }
  75. '
  76. $ curl -XPUT 'http://localhost:9200/transactions/stock/3' -d '
  77. {
  78. "type": "cost",
  79. "amount": 30
  80. }
  81. '
  82. $ curl -XPUT 'http://localhost:9200/transactions/stock/4' -d '
  83. {
  84. "type": "sale",
  85. "amount": 130
  86. }
  87. '
  88. --------------------------------------------------
  89. 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
  90. at each stage of the example above.
  91. ===== Before init_script
  92. No params object was specified so the default params object is used:
  93. [source,js]
  94. --------------------------------------------------
  95. "params" : {
  96. "_agg" : {}
  97. }
  98. --------------------------------------------------
  99. ===== After init_script
  100. This is run once on each shard before any document collection is performed, and so we will have a copy on each shard:
  101. Shard A::
  102. +
  103. [source,js]
  104. --------------------------------------------------
  105. "params" : {
  106. "_agg" : {
  107. "transactions" : []
  108. }
  109. }
  110. --------------------------------------------------
  111. Shard B::
  112. +
  113. [source,js]
  114. --------------------------------------------------
  115. "params" : {
  116. "_agg" : {
  117. "transactions" : []
  118. }
  119. }
  120. --------------------------------------------------
  121. ===== After map_script
  122. Each shard collects its documents and runs the map_script on each document that is collected:
  123. Shard A::
  124. +
  125. [source,js]
  126. --------------------------------------------------
  127. "params" : {
  128. "_agg" : {
  129. "transactions" : [ 80, -30 ]
  130. }
  131. }
  132. --------------------------------------------------
  133. Shard B::
  134. +
  135. [source,js]
  136. --------------------------------------------------
  137. "params" : {
  138. "_agg" : {
  139. "transactions" : [ -10, 130 ]
  140. }
  141. }
  142. --------------------------------------------------
  143. ===== After combine_script
  144. 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
  145. shard (by summing the values in the transactions array) which is passed back to the coordinating node:
  146. Shard A:: 50
  147. Shard B:: 120
  148. ===== After reduce_script
  149. The reduce_script receives an `_aggs` array containing the result of the combine script for each shard:
  150. [source,js]
  151. --------------------------------------------------
  152. "_aggs" : [
  153. 50,
  154. 120
  155. ]
  156. --------------------------------------------------
  157. 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
  158. produce the response:
  159. [source,js]
  160. --------------------------------------------------
  161. {
  162. ...
  163. "aggregations": {
  164. "profit": {
  165. "value": 170
  166. }
  167. }
  168. }
  169. --------------------------------------------------
  170. ==== Other Parameters
  171. [horizontal]
  172. params:: Optional. An object whose contents will be passed as variables to the `init_script`, `map_script` and `combine_script`. This can be
  173. useful to allow the user to control the behavior of the aggregation and for storing state between the scripts. If this is not specified,
  174. the default is the equivalent of providing:
  175. +
  176. [source,js]
  177. --------------------------------------------------
  178. "params" : {
  179. "_agg" : {}
  180. }
  181. --------------------------------------------------
  182. 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
  183. the behavior of the reduce phase. If this is not specified the variable will be undefined in the reduce_script execution.
  184. lang:: Optional. The script language used for the scripts. If this is not specified the default scripting language is used.
  185. init_script_file:: Optional. Can be used in place of the `init_script` parameter to provide the script using in a file.
  186. init_script_id:: Optional. Can be used in place of the `init_script` parameter to provide the script using an indexed script.
  187. map_script_file:: Optional. Can be used in place of the `map_script` parameter to provide the script using in a file.
  188. map_script_id:: Optional. Can be used in place of the `map_script` parameter to provide the script using an indexed script.
  189. combine_script_file:: Optional. Can be used in place of the `combine_script` parameter to provide the script using in a file.
  190. combine_script_id:: Optional. Can be used in place of the `combine_script` parameter to provide the script using an indexed script.
  191. reduce_script_file:: Optional. Can be used in place of the `reduce_script` parameter to provide the script using in a file.
  192. reduce_script_id:: Optional. Can be used in place of the `reduce_script` parameter to provide the script using an indexed script.