avg-aggregation.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. [[search-aggregations-metrics-avg-aggregation]]
  2. === Avg aggregation
  3. ++++
  4. <titleabbrev>Avg</titleabbrev>
  5. ++++
  6. A `single-value` metrics aggregation that computes the average of numeric values that are extracted from the aggregated documents. These values can be extracted either from specific numeric or <<histogram,histogram>> fields in the documents.
  7. Assuming the data consists of documents representing exams grades (between 0
  8. and 100) of students we can average their scores with:
  9. [source,console]
  10. --------------------------------------------------
  11. POST /exams/_search?size=0
  12. {
  13. "aggs": {
  14. "avg_grade": { "avg": { "field": "grade" } }
  15. }
  16. }
  17. --------------------------------------------------
  18. // TEST[setup:exams]
  19. The above aggregation computes the average grade over all documents. The aggregation type is `avg` and the `field` setting defines the numeric field of the documents the average will be computed on. The above will return the following:
  20. [source,console-result]
  21. --------------------------------------------------
  22. {
  23. ...
  24. "aggregations": {
  25. "avg_grade": {
  26. "value": 75.0
  27. }
  28. }
  29. }
  30. --------------------------------------------------
  31. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  32. The name of the aggregation (`avg_grade` above) also serves as the key by which the aggregation result can be retrieved from the returned response.
  33. ==== Script
  34. Let's say the exam was exceedingly difficult, and you need to apply a grade correction. Average a <<runtime,runtime field>> to get a corrected average:
  35. [source,console]
  36. ----
  37. POST /exams/_search?size=0
  38. {
  39. "runtime_mappings": {
  40. "grade.corrected": {
  41. "type": "double",
  42. "script": {
  43. "source": "emit(Math.min(100, doc['grade'].value * params.correction))",
  44. "params": {
  45. "correction": 1.2
  46. }
  47. }
  48. }
  49. },
  50. "aggs": {
  51. "avg_corrected_grade": {
  52. "avg": {
  53. "field": "grade.corrected"
  54. }
  55. }
  56. }
  57. }
  58. ----
  59. // TEST[setup:exams]
  60. // TEST[s/size=0/size=0&filter_path=aggregations/]
  61. ////
  62. [source,console-result]
  63. ----
  64. {
  65. "aggregations": {
  66. "avg_corrected_grade": {
  67. "value": 80.0
  68. }
  69. }
  70. }
  71. ----
  72. ////
  73. ==== Missing value
  74. The `missing` parameter defines how documents that are missing a value should be treated.
  75. By default they will be ignored but it is also possible to treat them as if they
  76. had a value.
  77. [source,console]
  78. --------------------------------------------------
  79. POST /exams/_search?size=0
  80. {
  81. "aggs": {
  82. "grade_avg": {
  83. "avg": {
  84. "field": "grade",
  85. "missing": 10 <1>
  86. }
  87. }
  88. }
  89. }
  90. --------------------------------------------------
  91. // TEST[setup:exams]
  92. <1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.
  93. [[search-aggregations-metrics-avg-aggregation-histogram-fields]]
  94. ==== Histogram fields
  95. When average is computed on <<histogram,histogram fields>>, the result of the aggregation is the weighted average
  96. of all elements in the `values` array taking into consideration the number in the same position in the `counts` array.
  97. For example, for the following index that stores pre-aggregated histograms with latency metrics for different networks:
  98. [source,console]
  99. --------------------------------------------------
  100. PUT metrics_index/_doc/1
  101. {
  102. "network.name" : "net-1",
  103. "latency_histo" : {
  104. "values" : [0.1, 0.2, 0.3, 0.4, 0.5], <1>
  105. "counts" : [3, 7, 23, 12, 6] <2>
  106. }
  107. }
  108. PUT metrics_index/_doc/2
  109. {
  110. "network.name" : "net-2",
  111. "latency_histo" : {
  112. "values" : [0.1, 0.2, 0.3, 0.4, 0.5], <1>
  113. "counts" : [8, 17, 8, 7, 6] <2>
  114. }
  115. }
  116. POST /metrics_index/_search?size=0
  117. {
  118. "aggs": {
  119. "avg_latency":
  120. { "avg": { "field": "latency_histo" }
  121. }
  122. }
  123. }
  124. --------------------------------------------------
  125. For each histogram field the `avg` aggregation adds each number in the `values` array <1> multiplied by its associated count
  126. in the `counts` array <2>. Eventually, it will compute the average over those values for all histograms and return the following result:
  127. [source,console-result]
  128. --------------------------------------------------
  129. {
  130. ...
  131. "aggregations": {
  132. "avg_latency": {
  133. "value": 0.29690721649
  134. }
  135. }
  136. }
  137. --------------------------------------------------
  138. // TESTRESPONSE[skip:test not setup]