stats-aggregation.asciidoc 4.6 KB

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