Skip to main content

Stats Aggregation

The Stats aggregation is a multi-value metrics aggregation that provides statistical information (count, sum, min, max and average of a field) over numeric values extracted from the aggregated documents.

In order to use the Stats aggregation import the following:

import zio.elasticsearch.aggregation.StatsAggregation
import zio.elasticsearch.ElasticAggregation.statsAggregation

You can create a Stats aggregation using the statsAggregation method this way:

val aggregation: StatsAggregation = statsAggregation(name = "statsAggregation", field = "intField")

You can create a type-safe Stats aggregation using the statsAggregation method this way:

// Document.intField must be number value, because of Stats aggregation
val aggregation: StatsAggregation = statsAggregation(name = "statsAggregation", field = Document.intField)

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

val aggregationWithMissing: StatsAggregation = statsAggregation(name = "statsAggregation", field = Document.intField).missing(10.0)

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

val multipleAggregations: MultipleAggregations = statsAggregation(name = "statsAggregation1", field = Document.intField).withAgg(statsAggregation(name = "statsAggregation2", field = Document.doubleField))

You can find more information about Stats aggregation here.