Skip to main content

Sum Aggregation

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

In order to use the Sum aggregation import the following:

import zio.elasticsearch.aggregation.SumAggregation
import zio.elasticsearch.ElasticAggregation.sumAggregation

You can create a Sum aggregation using the sumAggregation method this way:

val aggregation: SumAggregation = sumAggregation(name = "sumAggregation", field = "intField")

You can create a type-safe Sum aggregation using the sumAggregation method this way:

// Document.intField must be number value, because of Sum aggregation
val aggregation: SumAggregation = sumAggregation(name = "sumAggregation", field = Document.intField)

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

val aggregationWithMissing: SumAggregation = sumAggregation(name = "sumAggregation", field = Document.intField).missing(10.0)

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

val multipleAggregations: MultipleAggregations = sumAggregation(name = "sumAggregation1", field = Document.intField).withAgg(sumAggregation(name = "sumAggregation2", field = Document.doubleField))

You can find more information about Sum aggregation here.