sum-aggregation.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. [[search-aggregations-metrics-sum-aggregation]]
  2. === Sum aggregation
  3. ++++
  4. <titleabbrev>Sum</titleabbrev>
  5. ++++
  6. A `single-value` metrics aggregation that sums up numeric values that are extracted from the aggregated documents.
  7. These values can be extracted either from specific numeric or <<histogram,histogram>> fields in the documents,
  8. or be generated by a provided script.
  9. Assuming the data consists of documents representing sales records we can sum
  10. the sale price of all hats with:
  11. [source,console]
  12. --------------------------------------------------
  13. POST /sales/_search?size=0
  14. {
  15. "query": {
  16. "constant_score": {
  17. "filter": {
  18. "match": { "type": "hat" }
  19. }
  20. }
  21. },
  22. "aggs": {
  23. "hat_prices": { "sum": { "field": "price" } }
  24. }
  25. }
  26. --------------------------------------------------
  27. // TEST[setup:sales]
  28. Resulting in:
  29. [source,console-result]
  30. --------------------------------------------------
  31. {
  32. ...
  33. "aggregations": {
  34. "hat_prices": {
  35. "value": 450.0
  36. }
  37. }
  38. }
  39. --------------------------------------------------
  40. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  41. The name of the aggregation (`hat_prices` above) also serves as the key by which the aggregation result can be retrieved from the returned response.
  42. ==== Script
  43. We could also use a script to fetch the sales price:
  44. [source,console]
  45. --------------------------------------------------
  46. POST /sales/_search?size=0
  47. {
  48. "query": {
  49. "constant_score": {
  50. "filter": {
  51. "match": { "type": "hat" }
  52. }
  53. }
  54. },
  55. "aggs": {
  56. "hat_prices": {
  57. "sum": {
  58. "script": {
  59. "source": "doc.price.value"
  60. }
  61. }
  62. }
  63. }
  64. }
  65. --------------------------------------------------
  66. // TEST[setup:sales]
  67. This will interpret the `script` parameter as an `inline` script with the `painless` script language and no script parameters. To use a stored script use the following syntax:
  68. [source,console]
  69. --------------------------------------------------
  70. POST /sales/_search?size=0
  71. {
  72. "query": {
  73. "constant_score": {
  74. "filter": {
  75. "match": { "type": "hat" }
  76. }
  77. }
  78. },
  79. "aggs": {
  80. "hat_prices": {
  81. "sum": {
  82. "script": {
  83. "id": "my_script",
  84. "params": {
  85. "field": "price"
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. --------------------------------------------------
  93. // TEST[setup:sales,stored_example_script]
  94. ===== Value Script
  95. It is also possible to access the field value from the script using `_value`.
  96. For example, this will sum the square of the prices for all hats:
  97. [source,console]
  98. --------------------------------------------------
  99. POST /sales/_search?size=0
  100. {
  101. "query": {
  102. "constant_score": {
  103. "filter": {
  104. "match": { "type": "hat" }
  105. }
  106. }
  107. },
  108. "aggs": {
  109. "square_hats": {
  110. "sum": {
  111. "field": "price",
  112. "script": {
  113. "source": "_value * _value"
  114. }
  115. }
  116. }
  117. }
  118. }
  119. --------------------------------------------------
  120. // TEST[setup:sales]
  121. ==== Missing value
  122. The `missing` parameter defines how documents that are missing a value should
  123. be treated. By default documents missing the value will be ignored but it is
  124. also possible to treat them as if they had a value. For example, this treats
  125. all hat sales without a price as being `100`.
  126. [source,console]
  127. --------------------------------------------------
  128. POST /sales/_search?size=0
  129. {
  130. "query": {
  131. "constant_score": {
  132. "filter": {
  133. "match": { "type": "hat" }
  134. }
  135. }
  136. },
  137. "aggs": {
  138. "hat_prices": {
  139. "sum": {
  140. "field": "price",
  141. "missing": 100 <1>
  142. }
  143. }
  144. }
  145. }
  146. --------------------------------------------------
  147. // TEST[setup:sales]
  148. [[search-aggregations-metrics-sum-aggregation-histogram-fields]]
  149. ==== Histogram fields
  150. When sum is computed on <<histogram,histogram fields>>, the result of the aggregation is the sum of all elements in the `values`
  151. array multiplied by the number in the same position in the `counts` array.
  152. For example, for the following index that stores pre-aggregated histograms with latency metrics for different networks:
  153. [source,console]
  154. --------------------------------------------------
  155. PUT metrics_index/_doc/1
  156. {
  157. "network.name" : "net-1",
  158. "latency_histo" : {
  159. "values" : [0.1, 0.2, 0.3, 0.4, 0.5], <1>
  160. "counts" : [3, 7, 23, 12, 6] <2>
  161. }
  162. }
  163. PUT metrics_index/_doc/2
  164. {
  165. "network.name" : "net-2",
  166. "latency_histo" : {
  167. "values" : [0.1, 0.2, 0.3, 0.4, 0.5], <1>
  168. "counts" : [8, 17, 8, 7, 6] <2>
  169. }
  170. }
  171. POST /metrics_index/_search?size=0
  172. {
  173. "aggs" : {
  174. "total_latency" : { "sum" : { "field" : "latency_histo" } }
  175. }
  176. }
  177. --------------------------------------------------
  178. For each histogram field the `sum` aggregation will multiply each number in the `values` array <1> multiplied by its associated count
  179. in the `counts` array <2>. Eventually, it will add all values for all histograms and return the following result:
  180. [source,console-result]
  181. --------------------------------------------------
  182. {
  183. ...
  184. "aggregations": {
  185. "total_latency": {
  186. "value": 28.8
  187. }
  188. }
  189. }
  190. --------------------------------------------------
  191. // TESTRESPONSE[skip:test not setup]