extendedstats-aggregation.asciidoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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,console]
  7. --------------------------------------------------
  8. GET /exams/_search
  9. {
  10. "size": 0,
  11. "aggs": {
  12. "grades_stats": { "extended_stats": { "field": "grade" } }
  13. }
  14. }
  15. --------------------------------------------------
  16. // TEST[setup:exams]
  17. 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:
  18. The `std_deviation` and `variance` are calculated as population metrics so they are always the same as `std_deviation_population` and `variance_population` respectively.
  19. [source,console-result]
  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. "variance_population": 625.0,
  33. "variance_sampling": 1250.0,
  34. "std_deviation": 25.0,
  35. "std_deviation_population": 25.0,
  36. "std_deviation_sampling": 35.35533905932738,
  37. "std_deviation_bounds": {
  38. "upper": 125.0,
  39. "lower": 25.0,
  40. "upper_population": 125.0,
  41. "lower_population": 25.0,
  42. "upper_sampling": 145.71067811865476,
  43. "lower_sampling": 4.289321881345245
  44. }
  45. }
  46. }
  47. }
  48. --------------------------------------------------
  49. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  50. 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.
  51. ==== Standard Deviation Bounds
  52. By default, the `extended_stats` metric will return an object called `std_deviation_bounds`, which provides an interval of plus/minus two standard
  53. deviations from the mean. This can be a useful way to visualize variance of your data. If you want a different boundary, for example
  54. three standard deviations, you can set `sigma` in the request:
  55. [source,console]
  56. --------------------------------------------------
  57. GET /exams/_search
  58. {
  59. "size": 0,
  60. "aggs": {
  61. "grades_stats": {
  62. "extended_stats": {
  63. "field": "grade",
  64. "sigma": 3 <1>
  65. }
  66. }
  67. }
  68. }
  69. --------------------------------------------------
  70. // TEST[setup:exams]
  71. <1> `sigma` controls how many standard deviations +/- from the mean should be displayed
  72. `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
  73. return the average for both `upper` and `lower` bounds.
  74. The `upper` and `lower` bounds are calculated as population metrics so they are always the same as `upper_population` and
  75. `lower_population` respectively.
  76. .Standard Deviation and Bounds require normality
  77. [NOTE]
  78. =====
  79. The standard deviation and its bounds are displayed by default, but they are not always applicable to all data-sets. Your data must
  80. be normally distributed for the metrics to make sense. The statistics behind standard deviations assumes normally distributed data, so
  81. if your data is skewed heavily left or right, the value returned will be misleading.
  82. =====
  83. ==== Script
  84. Computing the grades stats based on a script:
  85. [source,console]
  86. --------------------------------------------------
  87. GET /exams/_search
  88. {
  89. "size": 0,
  90. "aggs": {
  91. "grades_stats": {
  92. "extended_stats": {
  93. "script": {
  94. "source": "doc['grade'].value",
  95. "lang": "painless"
  96. }
  97. }
  98. }
  99. }
  100. }
  101. --------------------------------------------------
  102. // TEST[setup:exams]
  103. 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:
  104. [source,console]
  105. --------------------------------------------------
  106. GET /exams/_search
  107. {
  108. "size": 0,
  109. "aggs": {
  110. "grades_stats": {
  111. "extended_stats": {
  112. "script": {
  113. "id": "my_script",
  114. "params": {
  115. "field": "grade"
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. --------------------------------------------------
  123. // TEST[setup:exams,stored_example_script]
  124. ===== Value Script
  125. 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:
  126. [source,console]
  127. --------------------------------------------------
  128. GET /exams/_search
  129. {
  130. "size": 0,
  131. "aggs": {
  132. "grades_stats": {
  133. "extended_stats": {
  134. "field": "grade",
  135. "script": {
  136. "lang": "painless",
  137. "source": "_value * params.correction",
  138. "params": {
  139. "correction": 1.2
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. --------------------------------------------------
  147. // TEST[setup:exams]
  148. ==== Missing value
  149. The `missing` parameter defines how documents that are missing a value should be treated.
  150. By default they will be ignored but it is also possible to treat them as if they
  151. had a value.
  152. [source,console]
  153. --------------------------------------------------
  154. GET /exams/_search
  155. {
  156. "size": 0,
  157. "aggs": {
  158. "grades_stats": {
  159. "extended_stats": {
  160. "field": "grade",
  161. "missing": 0 <1>
  162. }
  163. }
  164. }
  165. }
  166. --------------------------------------------------
  167. // TEST[setup:exams]
  168. <1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `0`.