﻿---
title: Aggregations
description: This page covers how to run aggregations with the Go client. For the full aggregations reference, see Aggregations in the Elasticsearch documentation...
url: https://www.elastic.co/elastic/docs-builder/docs/2910/reference/elasticsearch/clients/go/using-the-api/aggregations
products:
  - Elasticsearch
  - Elasticsearch Client
  - Elasticsearch Go Client
---

# Aggregations
This page covers how to run aggregations with the Go client. For the full aggregations reference, see [Aggregations](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/2910/explore-analyze/query-filter/aggregations) in the Elasticsearch documentation.
Given documents with a `price` field, we run a sum aggregation on `index_name`.
<tab-set>
  <tab-item title="Low-level API">
    ```go
    query := `{
      "size": 0,
      "aggs": {
        "total_prices": {
          "sum": {
            "field": "price"
          }
        }
      }
    }`

    res, err := client.Search(
        client.Search.WithIndex("index_name"),
        client.Search.WithBody(strings.NewReader(query)),
    )
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    ```
  </tab-item>

  <tab-item title="Fully-typed API">
    The `some` package provides helpers for inline pointers on primitive types:
    ```go
    import "github.com/elastic/go-elasticsearch/v9/typedapi/some"
    ```

    ```go
    totalPricesAgg, err := es.Search().
        Index("index_name").
        Request(
            &search.Request{
                Size: some.Int(0),
                Aggregations: map[string]types.Aggregations{
                    "total_prices": {
                        Sum: &types.SumAggregation{
                            Field: some.String("price"),
                        },
                    },
                },
            },
        ).Do(context.Background())
    ```
  </tab-item>

  <tab-item title="esdsl API">
    The [`esdsl`](https://www.elastic.co/elastic/docs-builder/docs/2910/reference/elasticsearch/clients/go/typed-api/esdsl) aggregation builders let you define aggregations with a fluent syntax:
    ```go
    import "github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
    ```

    ```go
    totalPricesAgg, err := es.Search().
        Index("index_name").
        Size(0).
        AddAggregation("total_prices",
            esdsl.NewSumAggregation().Field("price"),
        ).
        Do(context.Background())
    ```
  </tab-item>
</tab-set>