Loading

Aggregations

This page covers how to run aggregations with the Go client using the typed API and the esdsl builders. For the full aggregations reference, see Aggregations in the Elasticsearch documentation. For raw-JSON aggregations, see the low-level API.

import "github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
		

Given documents with a price field, we run a sum aggregation on index_name. The esdsl aggregation builders let you define aggregations with a fluent syntax:

totalPricesAgg, err := es.Search().
    Index("index_name").
    Size(0).
    AddAggregation("total_prices",
        esdsl.NewSumAggregation().Field("price"),
    ).
    Do(context.Background())
		
  1. Specifies the index name.
  2. Sets the size to 0 to retrieve only the result of the aggregation.
  3. Names the aggregation total_prices.
  4. NewSumAggregation() creates a sum aggregation builder. Chain .Field() to set the target field.