sum-aggregation.asciidoc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.
  8. Assuming the data consists of documents representing sales records we can sum
  9. the sale price of all hats with:
  10. [source,console]
  11. --------------------------------------------------
  12. POST /sales/_search?size=0
  13. {
  14. "query": {
  15. "constant_score": {
  16. "filter": {
  17. "match": { "type": "hat" }
  18. }
  19. }
  20. },
  21. "aggs": {
  22. "hat_prices": { "sum": { "field": "price" } }
  23. }
  24. }
  25. --------------------------------------------------
  26. // TEST[setup:sales]
  27. Resulting in:
  28. [source,console-result]
  29. --------------------------------------------------
  30. {
  31. ...
  32. "aggregations": {
  33. "hat_prices": {
  34. "value": 450.0
  35. }
  36. }
  37. }
  38. --------------------------------------------------
  39. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  40. 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.
  41. ==== Script
  42. If you need to get the `sum` for something more complex than a single
  43. field, run the aggregation on a <<runtime,runtime field>>.
  44. [source,console]
  45. ----
  46. POST /sales/_search?size=0
  47. {
  48. "runtime_mappings": {
  49. "price.weighted": {
  50. "type": "double",
  51. "script": """
  52. double price = doc['price'].value;
  53. if (doc['promoted'].value) {
  54. price *= 0.8;
  55. }
  56. emit(price);
  57. """
  58. }
  59. },
  60. "query": {
  61. "constant_score": {
  62. "filter": {
  63. "match": { "type": "hat" }
  64. }
  65. }
  66. },
  67. "aggs": {
  68. "hat_prices": {
  69. "sum": {
  70. "field": "price.weighted"
  71. }
  72. }
  73. }
  74. }
  75. ----
  76. // TEST[setup:sales]
  77. // TEST[s/size=0/size=0&filter_path=aggregations/]
  78. ////
  79. [source,console-result]
  80. ----
  81. {
  82. "aggregations": {
  83. "hat_prices": {
  84. "value": 370.0
  85. }
  86. }
  87. }
  88. ----
  89. ////
  90. ==== Missing value
  91. The `missing` parameter defines how documents that are missing a value should
  92. be treated. By default documents missing the value will be ignored but it is
  93. also possible to treat them as if they had a value. For example, this treats
  94. all hat sales without a price as being `100`.
  95. [source,console]
  96. --------------------------------------------------
  97. POST /sales/_search?size=0
  98. {
  99. "query": {
  100. "constant_score": {
  101. "filter": {
  102. "match": { "type": "hat" }
  103. }
  104. }
  105. },
  106. "aggs": {
  107. "hat_prices": {
  108. "sum": {
  109. "field": "price",
  110. "missing": 100 <1>
  111. }
  112. }
  113. }
  114. }
  115. --------------------------------------------------
  116. // TEST[setup:sales]
  117. [[search-aggregations-metrics-sum-aggregation-histogram-fields]]
  118. ==== Histogram fields
  119. When sum is computed on <<histogram,histogram fields>>, the result of the aggregation is the sum of all elements in the `values`
  120. array multiplied by the number in the same position in the `counts` array.
  121. For example, for the following index that stores pre-aggregated histograms with latency metrics for different networks:
  122. [source,console]
  123. --------------------------------------------------
  124. PUT metrics_index/_doc/1
  125. {
  126. "network.name" : "net-1",
  127. "latency_histo" : {
  128. "values" : [0.1, 0.2, 0.3, 0.4, 0.5], <1>
  129. "counts" : [3, 7, 23, 12, 6] <2>
  130. }
  131. }
  132. PUT metrics_index/_doc/2
  133. {
  134. "network.name" : "net-2",
  135. "latency_histo" : {
  136. "values" : [0.1, 0.2, 0.3, 0.4, 0.5], <1>
  137. "counts" : [8, 17, 8, 7, 6] <2>
  138. }
  139. }
  140. POST /metrics_index/_search?size=0
  141. {
  142. "aggs" : {
  143. "total_latency" : { "sum" : { "field" : "latency_histo" } }
  144. }
  145. }
  146. --------------------------------------------------
  147. For each histogram field the `sum` aggregation will multiply each number in the `values` array <1> multiplied by its associated count
  148. in the `counts` array <2>. Eventually, it will add all values for all histograms and return the following result:
  149. [source,console-result]
  150. --------------------------------------------------
  151. {
  152. ...
  153. "aggregations": {
  154. "total_latency": {
  155. "value": 28.8
  156. }
  157. }
  158. }
  159. --------------------------------------------------
  160. // TESTRESPONSE[skip:test not setup]