stats-aggregation.asciidoc 4.6 KB

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