stats-aggregation.asciidoc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. TIP: The `script` parameter expects an inline script. Use `script_id` for indexed scripts and `script_file` for scripts in the `config/scripts/` directory.
  43. ===== Value Script
  44. 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:
  45. [source,js]
  46. --------------------------------------------------
  47. {
  48. "aggs" : {
  49. ...
  50. "aggs" : {
  51. "grades_stats" : {
  52. "stats" : {
  53. "field" : "grade",
  54. "script" : "_value * correction",
  55. "params" : {
  56. "correction" : 1.2
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. --------------------------------------------------
  64. ==== Missing value
  65. The `missing` parameter defines how documents that are missing a value should be treated.
  66. By default they will be ignored but it is also possible to treat them as if they
  67. had a value.
  68. [source,js]
  69. --------------------------------------------------
  70. {
  71. "aggs" : {
  72. "grades_stats" : {
  73. "stats" : {
  74. "field" : "grade",
  75. "missing": 0 <1>
  76. }
  77. }
  78. }
  79. }
  80. --------------------------------------------------
  81. <1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `0`.