extendedstats-aggregation.asciidoc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. [[search-aggregations-metrics-extendedstats-aggregation]]
  2. === Extended 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 `extended_stats` aggregations is an extended version of the <<search-aggregations-metrics-stats-aggregation,`stats`>> aggregation, where additional metrics are added such as `sum_of_squares`, `variance`, `std_deviation` and `std_deviation_bounds`.
  5. Assuming the data consists of documents representing exams grades (between 0 and 100) of students
  6. [source,js]
  7. --------------------------------------------------
  8. GET /exams/_search
  9. {
  10. "size": 0,
  11. "aggs" : {
  12. "grades_stats" : { "extended_stats" : { "field" : "grade" } }
  13. }
  14. }
  15. --------------------------------------------------
  16. // CONSOLE
  17. // TEST[setup:exams]
  18. The above aggregation computes the grades statistics over all documents. The aggregation type is `extended_stats` and the `field` setting defines the numeric field of the documents the stats will be computed on. The above will return the following:
  19. [source,js]
  20. --------------------------------------------------
  21. {
  22. ...
  23. "aggregations": {
  24. "grades_stats": {
  25. "count": 2,
  26. "min": 50.0,
  27. "max": 100.0,
  28. "avg": 75.0,
  29. "sum": 150.0,
  30. "sum_of_squares": 12500.0,
  31. "variance": 625.0,
  32. "std_deviation": 25.0,
  33. "std_deviation_bounds": {
  34. "upper": 125.0,
  35. "lower": 25.0
  36. }
  37. }
  38. }
  39. }
  40. --------------------------------------------------
  41. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  42. 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.
  43. ==== Standard Deviation Bounds
  44. By default, the `extended_stats` metric will return an object called `std_deviation_bounds`, which provides an interval of plus/minus two standard
  45. deviations from the mean. This can be a useful way to visualize variance of your data. If you want a different boundary, for example
  46. three standard deviations, you can set `sigma` in the request:
  47. [source,js]
  48. --------------------------------------------------
  49. GET /exams/_search
  50. {
  51. "size": 0,
  52. "aggs" : {
  53. "grades_stats" : {
  54. "extended_stats" : {
  55. "field" : "grade",
  56. "sigma" : 3 <1>
  57. }
  58. }
  59. }
  60. }
  61. --------------------------------------------------
  62. // CONSOLE
  63. // TEST[setup:exams]
  64. <1> `sigma` controls how many standard deviations +/- from the mean should be displayed
  65. `sigma` can be any non-negative double, meaning you can request non-integer values such as `1.5`. A value of `0` is valid, but will simply
  66. return the average for both `upper` and `lower` bounds.
  67. .Standard Deviation and Bounds require normality
  68. [NOTE]
  69. =====
  70. The standard deviation and its bounds are displayed by default, but they are not always applicable to all data-sets. Your data must
  71. be normally distributed for the metrics to make sense. The statistics behind standard deviations assumes normally distributed data, so
  72. if your data is skewed heavily left or right, the value returned will be misleading.
  73. =====
  74. ==== Script
  75. Computing the grades stats based on a script:
  76. [source,js]
  77. --------------------------------------------------
  78. GET /exams/_search
  79. {
  80. "size": 0,
  81. "aggs" : {
  82. "grades_stats" : {
  83. "extended_stats" : {
  84. "script" : {
  85. "source" : "doc['grade'].value",
  86. "lang" : "painless"
  87. }
  88. }
  89. }
  90. }
  91. }
  92. --------------------------------------------------
  93. // CONSOLE
  94. // TEST[setup:exams]
  95. 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:
  96. [source,js]
  97. --------------------------------------------------
  98. GET /exams/_search
  99. {
  100. "size": 0,
  101. "aggs" : {
  102. "grades_stats" : {
  103. "extended_stats" : {
  104. "script" : {
  105. "id": "my_script",
  106. "params": {
  107. "field": "grade"
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. --------------------------------------------------
  115. // CONSOLE
  116. // TEST[setup:exams,stored_example_script]
  117. ===== Value Script
  118. 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 stats:
  119. [source,js]
  120. --------------------------------------------------
  121. GET /exams/_search
  122. {
  123. "size": 0,
  124. "aggs" : {
  125. "grades_stats" : {
  126. "extended_stats" : {
  127. "field" : "grade",
  128. "script" : {
  129. "lang" : "painless",
  130. "source": "_value * params.correction",
  131. "params" : {
  132. "correction" : 1.2
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. --------------------------------------------------
  140. // CONSOLE
  141. // TEST[setup:exams]
  142. ==== Missing value
  143. The `missing` parameter defines how documents that are missing a value should be treated.
  144. By default they will be ignored but it is also possible to treat them as if they
  145. had a value.
  146. [source,js]
  147. --------------------------------------------------
  148. GET /exams/_search
  149. {
  150. "size": 0,
  151. "aggs" : {
  152. "grades_stats" : {
  153. "extended_stats" : {
  154. "field" : "grade",
  155. "missing": 0 <1>
  156. }
  157. }
  158. }
  159. }
  160. --------------------------------------------------
  161. // CONSOLE
  162. // TEST[setup:exams]
  163. <1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `0`.