Skip to main content

Avg Aggregation

The Avg aggregation is a single-value metrics aggregation that keeps track and returns the average value among the numeric values extracted from the aggregated documents.

In order to use the Avg aggregation import the following:

import zio.elasticsearch.aggregation.AvgAggregation
import zio.elasticsearch.ElasticAggregation.avgAggregation

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

val aggregation: AvgAggregation = avgAggregation(name = "avgAggregation", field = "intField")

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

// Document.intField must be number value, because of Avg aggregation
val aggregation: AvgAggregation = avgAggregation(name = "avgAggregation", field = Document.intField)

If you want to change the missing parameter, you can use missing method:

val aggregationWithMissing: AvgAggregation = avgAggregation(name = "avgAggregation", field = Document.intField).missing(10.0)

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

val multipleAggregations: MultipleAggregations = avgAggregation(name = "avgAggregation1", field = Document.intField).withAgg(avgAggregation(name = "avgAggregation2", field = Document.doubleField))

You can find more information about Avg aggregation here.