sum-aggregation.asciidoc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. These values can be extracted either from specific numeric fields in the documents, or be generated by a provided script.
  4. Assuming the data consists of documents representing sales records we can sum
  5. the sale price of all hats with:
  6. [source,console]
  7. --------------------------------------------------
  8. POST /sales/_search?size=0
  9. {
  10. "query" : {
  11. "constant_score" : {
  12. "filter" : {
  13. "match" : { "type" : "hat" }
  14. }
  15. }
  16. },
  17. "aggs" : {
  18. "hat_prices" : { "sum" : { "field" : "price" } }
  19. }
  20. }
  21. --------------------------------------------------
  22. // TEST[setup:sales]
  23. Resulting in:
  24. [source,js]
  25. --------------------------------------------------
  26. {
  27. ...
  28. "aggregations": {
  29. "hat_prices": {
  30. "value": 450.0
  31. }
  32. }
  33. }
  34. --------------------------------------------------
  35. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  36. 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.
  37. ==== Script
  38. We could also use a script to fetch the sales price:
  39. [source,console]
  40. --------------------------------------------------
  41. POST /sales/_search?size=0
  42. {
  43. "query" : {
  44. "constant_score" : {
  45. "filter" : {
  46. "match" : { "type" : "hat" }
  47. }
  48. }
  49. },
  50. "aggs" : {
  51. "hat_prices" : {
  52. "sum" : {
  53. "script" : {
  54. "source": "doc.price.value"
  55. }
  56. }
  57. }
  58. }
  59. }
  60. --------------------------------------------------
  61. // TEST[setup:sales]
  62. 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:
  63. [source,console]
  64. --------------------------------------------------
  65. POST /sales/_search?size=0
  66. {
  67. "query" : {
  68. "constant_score" : {
  69. "filter" : {
  70. "match" : { "type" : "hat" }
  71. }
  72. }
  73. },
  74. "aggs" : {
  75. "hat_prices" : {
  76. "sum" : {
  77. "script" : {
  78. "id": "my_script",
  79. "params" : {
  80. "field" : "price"
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. --------------------------------------------------
  88. // TEST[setup:sales,stored_example_script]
  89. ===== Value Script
  90. It is also possible to access the field value from the script using `_value`.
  91. For example, this will sum the square of the prices for all hats:
  92. [source,console]
  93. --------------------------------------------------
  94. POST /sales/_search?size=0
  95. {
  96. "query" : {
  97. "constant_score" : {
  98. "filter" : {
  99. "match" : { "type" : "hat" }
  100. }
  101. }
  102. },
  103. "aggs" : {
  104. "square_hats" : {
  105. "sum" : {
  106. "field" : "price",
  107. "script" : {
  108. "source": "_value * _value"
  109. }
  110. }
  111. }
  112. }
  113. }
  114. --------------------------------------------------
  115. // TEST[setup:sales]
  116. ==== Missing value
  117. The `missing` parameter defines how documents that are missing a value should
  118. be treated. By default documents missing the value will be ignored but it is
  119. also possible to treat them as if they had a value. For example, this treats
  120. all hat sales without a price as being `100`.
  121. [source,console]
  122. --------------------------------------------------
  123. POST /sales/_search?size=0
  124. {
  125. "query" : {
  126. "constant_score" : {
  127. "filter" : {
  128. "match" : { "type" : "hat" }
  129. }
  130. }
  131. },
  132. "aggs" : {
  133. "hat_prices" : {
  134. "sum" : {
  135. "field" : "price",
  136. "missing": 100 <1>
  137. }
  138. }
  139. }
  140. }
  141. --------------------------------------------------
  142. // TEST[setup:sales]