weighted-avg-aggregation.asciidoc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. [[search-aggregations-metrics-weight-avg-aggregation]]
  2. === Weighted Avg Aggregation
  3. A `single-value` metrics aggregation that computes the weighted average of numeric values that are extracted from the aggregated documents.
  4. These values can be extracted either from specific numeric fields in the documents.
  5. When calculating a regular average, each datapoint has an equal "weight" ... it contributes equally to the final value. Weighted averages,
  6. on the other hand, weight each datapoint differently. The amount that each datapoint contributes to the final value is extracted from the
  7. document, or provided by a script.
  8. As a formula, a weighted average is the `∑(value * weight) / ∑(weight)`
  9. A regular average can be thought of as a weighted average where every value has an implicit weight of `1`.
  10. .`weighted_avg` Parameters
  11. |===
  12. |Parameter Name |Description |Required |Default Value
  13. |`value` | The configuration for the field or script that provides the values |Required |
  14. |`weight` | The configuration for the field or script that provides the weights |Required |
  15. |`format` | The numeric response formatter |Optional |
  16. |`value_type` | A hint about the values for pure scripts or unmapped fields |Optional |
  17. |===
  18. The `value` and `weight` objects have per-field specific configuration:
  19. .`value` Parameters
  20. |===
  21. |Parameter Name |Description |Required |Default Value
  22. |`field` | The field that values should be extracted from |Required |
  23. |`missing` | A value to use if the field is missing entirely |Optional |
  24. |`script` | A script which provides the values for the document. This is mutually exclusive with `field` |Optional
  25. |===
  26. .`weight` Parameters
  27. |===
  28. |Parameter Name |Description |Required |Default Value
  29. |`field` | The field that weights should be extracted from |Required |
  30. |`missing` | A weight to use if the field is missing entirely |Optional |
  31. |`script` | A script which provides the weights for the document. This is mutually exclusive with `field` |Optional
  32. |===
  33. ==== Examples
  34. If our documents have a `"grade"` field that holds a 0-100 numeric score, and a `"weight"` field which holds an arbitrary numeric weight,
  35. we can calculate the weighted average using:
  36. [source,js]
  37. --------------------------------------------------
  38. POST /exams/_search
  39. {
  40. "size": 0,
  41. "aggs" : {
  42. "weighted_grade": {
  43. "weighted_avg": {
  44. "value": {
  45. "field": "grade"
  46. },
  47. "weight": {
  48. "field": "weight"
  49. }
  50. }
  51. }
  52. }
  53. }
  54. --------------------------------------------------
  55. // CONSOLE
  56. // TEST[setup:exams]
  57. Which yields a response like:
  58. [source,js]
  59. --------------------------------------------------
  60. {
  61. ...
  62. "aggregations": {
  63. "weighted_grade": {
  64. "value": 70.0
  65. }
  66. }
  67. }
  68. --------------------------------------------------
  69. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  70. While multiple values-per-field are allowed, only one weight is allowed. If the aggregation encounters
  71. a document that has more than one weight (e.g. the weight field is a multi-valued field) it will throw an exception.
  72. If you have this situation, you will need to specify a `script` for the weight field, and use the script
  73. to combine the multiple values into a single value to be used.
  74. This single weight will be applied independently to each value extracted from the `value` field.
  75. This example show how a single document with multiple values will be averaged with a single weight:
  76. [source,js]
  77. --------------------------------------------------
  78. POST /exams/_doc?refresh
  79. {
  80. "grade": [1, 2, 3],
  81. "weight": 2
  82. }
  83. POST /exams/_search
  84. {
  85. "size": 0,
  86. "aggs" : {
  87. "weighted_grade": {
  88. "weighted_avg": {
  89. "value": {
  90. "field": "grade"
  91. },
  92. "weight": {
  93. "field": "weight"
  94. }
  95. }
  96. }
  97. }
  98. }
  99. --------------------------------------------------
  100. // CONSOLE
  101. // TEST
  102. The three values (`1`, `2`, and `3`) will be included as independent values, all with the weight of `2`:
  103. [source,js]
  104. --------------------------------------------------
  105. {
  106. ...
  107. "aggregations": {
  108. "weighted_grade": {
  109. "value": 2.0
  110. }
  111. }
  112. }
  113. --------------------------------------------------
  114. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  115. The aggregation returns `2.0` as the result, which matches what we would expect when calculating by hand:
  116. `((1*2) + (2*2) + (3*2)) / (2+2+2) == 2`
  117. ==== Script
  118. Both the value and the weight can be derived from a script, instead of a field. As a simple example, the following
  119. will add one to the grade and weight in the document using a script:
  120. [source,js]
  121. --------------------------------------------------
  122. POST /exams/_search
  123. {
  124. "size": 0,
  125. "aggs" : {
  126. "weighted_grade": {
  127. "weighted_avg": {
  128. "value": {
  129. "script": "doc.grade.value + 1"
  130. },
  131. "weight": {
  132. "script": "doc.weight.value + 1"
  133. }
  134. }
  135. }
  136. }
  137. }
  138. --------------------------------------------------
  139. // CONSOLE
  140. // TEST[setup:exams]
  141. ==== Missing values
  142. The `missing` parameter defines how documents that are missing a value should be treated.
  143. The default behavior is different for `value` and `weight`:
  144. By default, if the `value` field is missing the document is ignored and the aggregation moves on to the next document.
  145. If the `weight` field is missing, it is assumed to have a weight of `1` (like a normal average).
  146. Both of these defaults can be overridden with the `missing` parameter:
  147. [source,js]
  148. --------------------------------------------------
  149. POST /exams/_search
  150. {
  151. "size": 0,
  152. "aggs" : {
  153. "weighted_grade": {
  154. "weighted_avg": {
  155. "value": {
  156. "field": "grade",
  157. "missing": 2
  158. },
  159. "weight": {
  160. "field": "weight",
  161. "missing": 3
  162. }
  163. }
  164. }
  165. }
  166. }
  167. --------------------------------------------------
  168. // CONSOLE
  169. // TEST[setup:exams]