stats-aggregation.asciidoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. [[search-aggregations-matrix-stats-aggregation]]
  2. === Matrix stats aggregation
  3. The `matrix_stats` aggregation is a numeric aggregation that computes the following statistics over a set of document fields:
  4. [horizontal]
  5. `count`:: Number of per field samples included in the calculation.
  6. `mean`:: The average value for each field.
  7. `variance`:: Per field Measurement for how spread out the samples are from the mean.
  8. `skewness`:: Per field measurement quantifying the asymmetric distribution around the mean.
  9. `kurtosis`:: Per field measurement quantifying the shape of the distribution.
  10. `covariance`:: A matrix that quantitatively describes how changes in one field are associated with another.
  11. `correlation`:: The covariance matrix scaled to a range of -1 to 1, inclusive. Describes the relationship between field
  12. distributions.
  13. IMPORTANT: Unlike other metric aggregations, the `matrix_stats` aggregation does
  14. not support scripting.
  15. //////////////////////////
  16. [source,js]
  17. --------------------------------------------------
  18. PUT /statistics/_doc/0
  19. {"poverty": 24.0, "income": 50000.0}
  20. PUT /statistics/_doc/1
  21. {"poverty": 13.0, "income": 95687.0}
  22. PUT /statistics/_doc/2
  23. {"poverty": 69.0, "income": 7890.0}
  24. POST /_refresh
  25. --------------------------------------------------
  26. // NOTCONSOLE
  27. // TESTSETUP
  28. //////////////////////////
  29. The following example demonstrates the use of matrix stats to describe the relationship between income and poverty.
  30. [source,console,id=stats-aggregation-example]
  31. --------------------------------------------------
  32. GET /_search
  33. {
  34. "aggs": {
  35. "statistics": {
  36. "matrix_stats": {
  37. "fields": [ "poverty", "income" ]
  38. }
  39. }
  40. }
  41. }
  42. --------------------------------------------------
  43. // TEST[s/_search/_search\?filter_path=aggregations/]
  44. The aggregation type is `matrix_stats` and the `fields` setting defines the set of fields (as an array) for computing
  45. the statistics. The above request returns the following response:
  46. [source,console-result]
  47. --------------------------------------------------
  48. {
  49. ...
  50. "aggregations": {
  51. "statistics": {
  52. "doc_count": 50,
  53. "fields": [ {
  54. "name": "income",
  55. "count": 50,
  56. "mean": 51985.1,
  57. "variance": 7.383377037755103E7,
  58. "skewness": 0.5595114003506483,
  59. "kurtosis": 2.5692365287787124,
  60. "covariance": {
  61. "income": 7.383377037755103E7,
  62. "poverty": -21093.65836734694
  63. },
  64. "correlation": {
  65. "income": 1.0,
  66. "poverty": -0.8352655256272504
  67. }
  68. }, {
  69. "name": "poverty",
  70. "count": 50,
  71. "mean": 12.732000000000001,
  72. "variance": 8.637730612244896,
  73. "skewness": 0.4516049811903419,
  74. "kurtosis": 2.8615929677997767,
  75. "covariance": {
  76. "income": -21093.65836734694,
  77. "poverty": 8.637730612244896
  78. },
  79. "correlation": {
  80. "income": -0.8352655256272504,
  81. "poverty": 1.0
  82. }
  83. } ]
  84. }
  85. }
  86. }
  87. --------------------------------------------------
  88. // TESTRESPONSE[s/\.\.\.//]
  89. // TESTRESPONSE[s/: (\-)?[0-9\.E]+/: $body.$_path/]
  90. The `doc_count` field indicates the number of documents involved in the computation of the statistics.
  91. ==== Multi Value Fields
  92. The `matrix_stats` aggregation treats each document field as an independent sample. The `mode` parameter controls what
  93. array value the aggregation will use for array or multi-valued fields. This parameter can take one of the following:
  94. [horizontal]
  95. `avg`:: (default) Use the average of all values.
  96. `min`:: Pick the lowest value.
  97. `max`:: Pick the highest value.
  98. `sum`:: Use the sum of all values.
  99. `median`:: Use the median of all values.
  100. ==== Missing Values
  101. The `missing` parameter defines how documents that are missing a value should be treated.
  102. By default they will be ignored but it is also possible to treat them as if they had a value.
  103. This is done by adding a set of fieldname : value mappings to specify default values per field.
  104. [source,console,id=stats-aggregation-missing-example]
  105. --------------------------------------------------
  106. GET /_search
  107. {
  108. "aggs": {
  109. "matrixstats": {
  110. "matrix_stats": {
  111. "fields": [ "poverty", "income" ],
  112. "missing": { "income": 50000 } <1>
  113. }
  114. }
  115. }
  116. }
  117. --------------------------------------------------
  118. <1> Documents without a value in the `income` field will have the default value `50000`.
  119. ==== Script
  120. This aggregation family does not yet support scripting.