avg-aggregation.asciidoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. [[search-aggregations-metrics-avg-aggregation]]
  2. === Avg Aggregation
  3. 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 fields in the documents, or be generated by a provided script.
  4. Assuming the data consists of documents representing exams grades (between 0
  5. and 100) of students we can average their scores with:
  6. [source,js]
  7. --------------------------------------------------
  8. POST /exams/_search?size=0
  9. {
  10. "aggs" : {
  11. "avg_grade" : { "avg" : { "field" : "grade" } }
  12. }
  13. }
  14. --------------------------------------------------
  15. // CONSOLE
  16. // TEST[setup:exams]
  17. 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:
  18. [source,js]
  19. --------------------------------------------------
  20. {
  21. ...
  22. "aggregations": {
  23. "avg_grade": {
  24. "value": 75.0
  25. }
  26. }
  27. }
  28. --------------------------------------------------
  29. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  30. 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.
  31. ==== Script
  32. Computing the average grade based on a script:
  33. [source,js]
  34. --------------------------------------------------
  35. POST /exams/_search?size=0
  36. {
  37. "aggs" : {
  38. "avg_grade" : {
  39. "avg" : {
  40. "script" : {
  41. "source" : "doc.grade.value"
  42. }
  43. }
  44. }
  45. }
  46. }
  47. --------------------------------------------------
  48. // CONSOLE
  49. // TEST[setup:exams]
  50. 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:
  51. [source,js]
  52. --------------------------------------------------
  53. POST /exams/_search?size=0
  54. {
  55. "aggs" : {
  56. "avg_grade" : {
  57. "avg" : {
  58. "script" : {
  59. "id": "my_script",
  60. "params": {
  61. "field": "grade"
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. --------------------------------------------------
  69. // CONSOLE
  70. // TEST[setup:exams,stored_example_script]
  71. ===== Value Script
  72. It turned out that the exam was way above the level of the students and a grade correction needs to be applied. We can use value script to get the new average:
  73. [source,js]
  74. --------------------------------------------------
  75. POST /exams/_search?size=0
  76. {
  77. "aggs" : {
  78. "avg_corrected_grade" : {
  79. "avg" : {
  80. "field" : "grade",
  81. "script" : {
  82. "lang": "painless",
  83. "source": "_value * params.correction",
  84. "params" : {
  85. "correction" : 1.2
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. --------------------------------------------------
  93. // CONSOLE
  94. // TEST[setup:exams]
  95. ==== Missing value
  96. The `missing` parameter defines how documents that are missing a value should be treated.
  97. By default they will be ignored but it is also possible to treat them as if they
  98. had a value.
  99. [source,js]
  100. --------------------------------------------------
  101. POST /exams/_search?size=0
  102. {
  103. "aggs" : {
  104. "grade_avg" : {
  105. "avg" : {
  106. "field" : "grade",
  107. "missing": 10 <1>
  108. }
  109. }
  110. }
  111. }
  112. --------------------------------------------------
  113. // CONSOLE
  114. // TEST[setup:exams]
  115. <1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.