|
@@ -5,11 +5,11 @@
|
|
|
++++
|
|
|
|
|
|
A `single-value` metrics aggregation that computes the weighted average of numeric values that are extracted from the aggregated documents.
|
|
|
-These values can be extracted either from specific numeric fields in the documents, or provided by a script.
|
|
|
+These values can be extracted either from specific numeric fields in the documents.
|
|
|
|
|
|
When calculating a regular average, each datapoint has an equal "weight" ... it contributes equally to the final value. Weighted averages,
|
|
|
on the other hand, weight each datapoint differently. The amount that each datapoint contributes to the final value is extracted from the
|
|
|
-document, or provided by a script.
|
|
|
+document.
|
|
|
|
|
|
As a formula, a weighted average is the `∑(value * weight) / ∑(weight)`
|
|
|
|
|
@@ -23,7 +23,6 @@ A regular average can be thought of as a weighted average where every value has
|
|
|
|`value` | The configuration for the field or script that provides the values |Required |
|
|
|
|`weight` | The configuration for the field or script that provides the weights |Required |
|
|
|
|`format` | The numeric response formatter |Optional |
|
|
|
-|`value_type` | A hint about the values for pure scripts or unmapped fields |Optional |
|
|
|
|===
|
|
|
|
|
|
The `value` and `weight` objects have per-field specific configuration:
|
|
@@ -35,7 +34,6 @@ The `value` and `weight` objects have per-field specific configuration:
|
|
|
|Parameter Name |Description |Required |Default Value
|
|
|
|`field` | The field that values should be extracted from |Required |
|
|
|
|`missing` | A value to use if the field is missing entirely |Optional |
|
|
|
-|`script` | A script which provides the values for the document. This is mutually exclusive with `field` |Optional
|
|
|
|===
|
|
|
|
|
|
[[weight-params]]
|
|
@@ -45,7 +43,6 @@ The `value` and `weight` objects have per-field specific configuration:
|
|
|
|Parameter Name |Description |Required |Default Value
|
|
|
|`field` | The field that weights should be extracted from |Required |
|
|
|
|`missing` | A weight to use if the field is missing entirely |Optional |
|
|
|
-|`script` | A script which provides the weights for the document. This is mutually exclusive with `field` |Optional
|
|
|
|===
|
|
|
|
|
|
|
|
@@ -92,9 +89,9 @@ Which yields a response like:
|
|
|
|
|
|
|
|
|
While multiple values-per-field are allowed, only one weight is allowed. If the aggregation encounters
|
|
|
-a document that has more than one weight (e.g. the weight field is a multi-valued field) it will throw an exception.
|
|
|
-If you have this situation, you will need to specify a `script` for the weight field, and use the script
|
|
|
-to combine the multiple values into a single value to be used.
|
|
|
+a document that has more than one weight (e.g. the weight field is a multi-valued field) it will abort the search.
|
|
|
+If you have this situation, you should build a <<search-aggregations-metrics-weight-avg-aggregation-runtime-field>>
|
|
|
+to combine those values into a single weight.
|
|
|
|
|
|
This single weight will be applied independently to each value extracted from the `value` field.
|
|
|
|
|
@@ -145,16 +142,40 @@ The three values (`1`, `2`, and `3`) will be included as independent values, all
|
|
|
The aggregation returns `2.0` as the result, which matches what we would expect when calculating by hand:
|
|
|
`((1*2) + (2*2) + (3*2)) / (2+2+2) == 2`
|
|
|
|
|
|
-==== Script
|
|
|
+[[search-aggregations-metrics-weight-avg-aggregation-runtime-field]]
|
|
|
+==== Runtime field
|
|
|
|
|
|
-Both the value and the weight can be derived from a script, instead of a field. As a simple example, the following
|
|
|
-will add one to the grade and weight in the document using a script:
|
|
|
+If you have to sum or weigh values that don't quite line up with the indexed
|
|
|
+values, run the aggregation on a <<runtime,runtime field>>.
|
|
|
|
|
|
[source,console]
|
|
|
---------------------------------------------------
|
|
|
-POST /exams/_search
|
|
|
+----
|
|
|
+POST /exams/_doc?refresh
|
|
|
+{
|
|
|
+ "grade": 100,
|
|
|
+ "weight": [2, 3]
|
|
|
+}
|
|
|
+POST /exams/_doc?refresh
|
|
|
+{
|
|
|
+ "grade": 80,
|
|
|
+ "weight": 3
|
|
|
+}
|
|
|
+
|
|
|
+POST /exams/_search?filter_path=aggregations
|
|
|
{
|
|
|
"size": 0,
|
|
|
+ "runtime_mappings": {
|
|
|
+ "weight.combined": {
|
|
|
+ "type": "double",
|
|
|
+ "script": """
|
|
|
+ double s = 0;
|
|
|
+ for (double w : doc['weight']) {
|
|
|
+ s += w;
|
|
|
+ }
|
|
|
+ emit(s);
|
|
|
+ """
|
|
|
+ }
|
|
|
+ },
|
|
|
"aggs": {
|
|
|
"weighted_grade": {
|
|
|
"weighted_avg": {
|
|
@@ -162,14 +183,26 @@ POST /exams/_search
|
|
|
"script": "doc.grade.value + 1"
|
|
|
},
|
|
|
"weight": {
|
|
|
- "script": "doc.weight.value + 1"
|
|
|
+ "field": "weight.combined"
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
---------------------------------------------------
|
|
|
-// TEST[setup:exams]
|
|
|
+----
|
|
|
+
|
|
|
+Which should look like:
|
|
|
+
|
|
|
+[source,console-result]
|
|
|
+----
|
|
|
+{
|
|
|
+ "aggregations": {
|
|
|
+ "weighted_grade": {
|
|
|
+ "value": 93.5
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+----
|
|
|
|
|
|
|
|
|
==== Missing values
|
|
@@ -204,4 +237,3 @@ POST /exams/_search
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
// TEST[setup:exams]
|
|
|
-
|