Skip to main content

Weighted Avg Aggregation

The Weighted Avg aggregation is a single-value metrics aggregation that computes the average while taking into account the varying degrees of importance of numeric values. As a formula, a weighted average is the ∑(value * weight) / ∑(weight)

In order to use the Weighted Avg aggregation import the following:

import zio.elasticsearch.aggregation.WeightedAvgAggregation
import zio.elasticsearch.ElasticAggregation.weightedAvgAggregation

You can create a Weighted Avg aggregation using the weightedAvgAggregation method this way:

val aggregation: WeightedAvgAggregation = weightedAvgAggregation(name = "weightedAvgAggregation", valueField = "doubleField", weightField = "intField")

You can create a type-safe Weighted Avg aggregation using the weightedAvgAggregation method this way:

val aggregation: WeightedAvgAggregation = weightedAvgAggregation(name = "weightedAvgAggregation", valueField = Document.doubleField, weightField = Document.intField)

If you want to add aggregation (on the same level), you can use withAgg method:

val multipleAggregations: MultipleAggregations = weightedAvgAggregation(name = "weightedAvgAggregation1", valueField = Document.intField, weightField = Document.doubleField).withAgg(weightedAvgAggregation(name = "weightedAvgAggregation2", valueField = Document.doubleField, weightField = Document.intField))

If you want to change the valueMissing, you can use valueMissing method:

val aggregationWithValueMissing: WeightedAvgAggregation = weightedAvgAggregation(name = "weightedAvgAggregation", field = Document.intField).valueMissing(10.0)

If you want to change the weightMissing, you can use weightMissing method:

val aggregationWithWeightMissing: WeightedAvgAggregation = weightedAvgAggregation(name = "weightedAvgAggregation", field = Document.intField).weightMissing(5.0)

If you want to change the weightMissing and valueMissing, you can use weightMissing and valueMissing methods:

val aggregationWithValueAndWeightMissing: WeightedAvgAggregation = weightedAvgAggregation(name = "weightedAvgAggregation", field = Document.intField).valueMissing(5.0).weightMissing(10.0)

You can find more information about Weighted Avg aggregation here.