stats-aggregation.asciidoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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" : { "stats" : { "script" : "doc['grade'].value" } }
  39. }
  40. }
  41. --------------------------------------------------
  42. This will interpret the `script` parameter as an `inline` script with the default script language and no script parameters. To use a file script use the following syntax:
  43. [source,js]
  44. --------------------------------------------------
  45. {
  46. ...,
  47. "aggs" : {
  48. "grades_stats" : {
  49. "stats" : {
  50. "script" : {
  51. "file": "my_script",
  52. "params" : {
  53. "field" : "grade"
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. --------------------------------------------------
  61. TIP: for indexed scripts replace the `file` parameter with an `id` parameter.
  62. ===== Value Script
  63. 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:
  64. [source,js]
  65. --------------------------------------------------
  66. {
  67. "aggs" : {
  68. ...
  69. "aggs" : {
  70. "grades_stats" : {
  71. "stats" : {
  72. "field" : "grade",
  73. "script" :
  74. "inline": "_value * correction",
  75. "params" : {
  76. "correction" : 1.2
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. --------------------------------------------------
  85. ==== Missing value
  86. The `missing` parameter defines how documents that are missing a value should be treated.
  87. By default they will be ignored but it is also possible to treat them as if they
  88. had a value.
  89. [source,js]
  90. --------------------------------------------------
  91. {
  92. "aggs" : {
  93. "grades_stats" : {
  94. "stats" : {
  95. "field" : "grade",
  96. "missing": 0 <1>
  97. }
  98. }
  99. }
  100. }
  101. --------------------------------------------------
  102. <1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `0`.