stats-aggregation.asciidoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. [[search-aggregations-metrics-stats-aggregation]]
  2. === Stats Aggregation
  3. A `multi-value` metrics aggregation that computes stats over numeric values 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. The stats that are returned consist of: `min`, `max`, `sum`, `count` and `avg`.
  5. Assuming the data consists of documents representing exams grades (between 0 and 100) of students
  6. [source,js]
  7. --------------------------------------------------
  8. {
  9. "aggs" : {
  10. "grades_stats" : { "stats" : { "field" : "grade" } }
  11. }
  12. }
  13. --------------------------------------------------
  14. The above aggregation computes the grades statistics over all documents. The aggregation type is `stats` and the `field` setting defines the numeric field of the documents the stats will be computed on. The above will return the following:
  15. [source,js]
  16. --------------------------------------------------
  17. {
  18. ...
  19. "aggregations": {
  20. "grades_stats": {
  21. "count": 6,
  22. "min": 60,
  23. "max": 98,
  24. "avg": 78.5,
  25. "sum": 471
  26. }
  27. }
  28. }
  29. --------------------------------------------------
  30. The name of the aggregation (`grades_stats` above) also serves as the key by which the aggregation result can be retrieved from the returned response.
  31. ==== Script
  32. Computing the grades stats based on a script:
  33. [source,js]
  34. --------------------------------------------------
  35. {
  36. ...,
  37. "aggs" : {
  38. "grades_stats" : {
  39. "stats" : {
  40. "script" : {
  41. "lang": "painless",
  42. "inline": "doc['grade'].value"
  43. }
  44. }
  45. }
  46. }
  47. }
  48. --------------------------------------------------
  49. This will interpret the `script` parameter as an `inline` script with the `painless` script language and no script parameters. To use a file script use the following syntax:
  50. [source,js]
  51. --------------------------------------------------
  52. {
  53. ...,
  54. "aggs" : {
  55. "grades_stats" : {
  56. "stats" : {
  57. "script" : {
  58. "file": "my_script",
  59. "params" : {
  60. "field" : "grade"
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. --------------------------------------------------
  68. TIP: for indexed scripts replace the `file` parameter with an `id` parameter.
  69. ===== Value Script
  70. 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 a value script to get the new stats:
  71. [source,js]
  72. --------------------------------------------------
  73. {
  74. "aggs" : {
  75. ...
  76. "aggs" : {
  77. "grades_stats" : {
  78. "stats" : {
  79. "field" : "grade",
  80. "script" :
  81. "lang": "painless",
  82. "inline": "_value * params.correction",
  83. "params" : {
  84. "correction" : 1.2
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. --------------------------------------------------
  93. ==== Missing value
  94. The `missing` parameter defines how documents that are missing a value should be treated.
  95. By default they will be ignored but it is also possible to treat them as if they
  96. had a value.
  97. [source,js]
  98. --------------------------------------------------
  99. {
  100. "aggs" : {
  101. "grades_stats" : {
  102. "stats" : {
  103. "field" : "grade",
  104. "missing": 0 <1>
  105. }
  106. }
  107. }
  108. }
  109. --------------------------------------------------
  110. <1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `0`.