sum-aggregation.asciidoc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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,js]
  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. // CONSOLE
  23. // TEST[setup:sales]
  24. Resulting in:
  25. [source,js]
  26. --------------------------------------------------
  27. {
  28. ...
  29. "aggregations": {
  30. "hat_prices": {
  31. "value": 450.0
  32. }
  33. }
  34. }
  35. --------------------------------------------------
  36. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  37. 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.
  38. ==== Script
  39. We could also use a script to fetch the sales price:
  40. [source,js]
  41. --------------------------------------------------
  42. POST /sales/_search?size=0
  43. {
  44. "query" : {
  45. "constant_score" : {
  46. "filter" : {
  47. "match" : { "type" : "hat" }
  48. }
  49. }
  50. },
  51. "aggs" : {
  52. "hat_prices" : {
  53. "sum" : {
  54. "script" : {
  55. "source": "doc.price.value"
  56. }
  57. }
  58. }
  59. }
  60. }
  61. --------------------------------------------------
  62. // CONSOLE
  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,js]
  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. // CONSOLE
  91. // TEST[setup:sales,stored_example_script]
  92. ===== Value Script
  93. It is also possible to access the field value from the script using `_value`.
  94. For example, this will sum the square of the prices for all hats:
  95. [source,js]
  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. "square_hats" : {
  108. "sum" : {
  109. "field" : "price",
  110. "script" : {
  111. "source": "_value * _value"
  112. }
  113. }
  114. }
  115. }
  116. }
  117. --------------------------------------------------
  118. // CONSOLE
  119. // TEST[setup:sales]
  120. ==== Missing value
  121. The `missing` parameter defines how documents that are missing a value should
  122. be treated. By default documents missing the value will be ignored but it is
  123. also possible to treat them as if they had a value. For example, this treats
  124. all hat sales without a price as being `100`.
  125. [source,js]
  126. --------------------------------------------------
  127. POST /sales/_search?size=0
  128. {
  129. "query" : {
  130. "constant_score" : {
  131. "filter" : {
  132. "match" : { "type" : "hat" }
  133. }
  134. }
  135. },
  136. "aggs" : {
  137. "hat_prices" : {
  138. "sum" : {
  139. "field" : "price",
  140. "missing": 100 <1>
  141. }
  142. }
  143. }
  144. }
  145. --------------------------------------------------
  146. // CONSOLE
  147. // TEST[setup:sales]