sum-aggregation.asciidoc 5.1 KB

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