A weighted average is ∑(value * weight) / ∑(weight)
Solution
Added new WeightedAverageAggregation class which extends MetricsAggregationBase.
As the structure of the weight average aggregation is slightly different from other metric aggregations. Add two new methods value and weight to allow the user to set these metrics. The metric can be a string (for a field name), or a script. They both accept an optional second param to set the missing property. Alternatively, the user can set value and weight when constructing the class - but cannot set missing.
Overwrote default methods script, field, and missing as these did not make sense as top level methods for this aggregation.
const weightedAvgAgg = new WeightedAverageAggregation('my_weighted_agg')
.value('my_value_field', 5)
.weight('my_weight_field', 10);
const body = new RequestBodySearch().agg(weightedAvgAgg);
Issues
resolves #154
Aim
Add support for weight average aggregations released in v6.4.0 release notes
A weighted average is
∑(value * weight) / ∑(weight)
Solution
WeightedAverageAggregation
class which extendsMetricsAggregationBase
.value
andweight
to allow the user to set these metrics. The metric can be a string (for a field name), or a script. They both accept an optional second param to set the missing property. Alternatively, the user can setvalue
andweight
when constructing the class - but cannot set missing.script
,field
, andmissing
as these did not make sense as top level methods for this aggregation.Example
Would produce a query with:
Alternatively.
Would produce a query with: