# Can't sum bucket after composite

**URL:** https://discuss.elastic.co/t/cant-sum-bucket-after-composite/300184
**Category:** Elasticsearch
**Created:** [March 21, 2022, 10:12am UTC](https://discuss.elastic.co/t/cant-sum-bucket-after-composite/300184 "2022-03-21T10:12:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Techinp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/techinp/32/78389_2.png) [@Techinp](https://discuss.elastic.co/u/Techinp)
#### Post date: [March 21, 2022, 10:12am UTC](https://discuss.elastic.co/t/cant-sum-bucket-after-composite/300184/1 "2022-03-21T10:12:17Z")

</div>

Hi all,  
How can I sum data after composite stage?  
What did I do wrong?

This is my query

```auto
GET report-20220316/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "parentId.keyword": "aaaa"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "username.keyword": "test"
          }
        }
      ]
    }
  },
  "aggs": {
    "group": {
      "composite": {
        "sources": [
          {
            "user": {
              "terms": {
                "field": "username.keyword",
                "order": "asc"
              }
            }
          }
        ]
      },
      "aggs": {
        "summary_total_price": {
          "sum": {
            "field": "summary.total.price"
          }
        }
      }
    },
    "summary_price": {
      "sum_bucket": {
        "buckets_path": "group>summary_total_price"
      }
    }
  }
}

```

Expected result

```auto
{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1027,
      "relation" : "eq"
    },
    "max_score" : 6.3193836,
    "hits" : [] 
  },
  "aggregations" : {
    "group" : {
      "after_key" : {
        "user" : "cc"
      },
      "buckets" : [
        {
          "key" : {
            "user" : "aa"
          },
          "doc_count" : 1,
          "summary_total_price" : {
            "value" : 10
          },
        },
        {
          "key" : {
            "user" : "bb"
          },
          "doc_count" : 34,
          "summary_total_price" : {
            "value" : 20,
          },
        },
        {
          "key" : {
            "user" : "cc"
          },
          "doc_count" : 4,
          "summary_total_price" : {
            "value" : 30,
          },
        },
      ]
    },
    "summary_price": {
      "value": 60.0
    }
  }
}

```

but got

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "action_request_validation_exception",
        "reason" : "Validation Failed: 1: The first aggregation in buckets_path must be a multi-bucket aggregation for aggregation [summary_price] found :org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder for buckets path: group>summary_price;"
      }
    ],
    "type" : "action_request_validation_exception",
    "reason" : "Validation Failed: 1: The first aggregation in buckets_path must be a multi-bucket aggregation for aggregation [summary_price] found :org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder for buckets path: group>summary_price;"
  },
  "status" : 400
}

```

Thanks

---

<div class="post-metadata">

### Author: ![casterQ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casterq/32/93257_2.png) [@casterQ](https://discuss.elastic.co/u/casterQ)
#### Post date: [March 21, 2022, 11:27am UTC](https://discuss.elastic.co/t/cant-sum-bucket-after-composite/300184/2 "2022-03-21T11:27:29Z")

</div>

The composite agg is not currently compatible with pipeline aggregations， please read this doc：

> **[Composite aggregation | Elasticsearch Guide \[8.1\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.1/search-aggregations-bucket-composite-aggregation.html#search-aggregations-bucket-composite-aggregation-pipeline-aggregations)**

---

<div class="post-metadata">

### Author: ![Techinp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/techinp/32/78389_2.png) [@Techinp](https://discuss.elastic.co/u/Techinp)
#### Post date: [March 21, 2022, 11:35am UTC](https://discuss.elastic.co/t/cant-sum-bucket-after-composite/300184/3 "2022-03-21T11:35:23Z")

</div>

Are there another way for summary data without using aggregations?

---

<div class="post-metadata">

### Author: ![casterQ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casterq/32/93257_2.png) [@casterQ](https://discuss.elastic.co/u/casterQ)
#### Post date: [March 22, 2022, 3:06am UTC](https://discuss.elastic.co/t/cant-sum-bucket-after-composite/300184/4 "2022-03-22T03:06:49Z")

</div>

What's the difference between the two？

1. sum `summary.total.price` directly
2. sum (sum `summary.total.price` after grouping by `username.keyword`)

---

<div class="post-metadata">

### Author: ![casterQ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casterq/32/93257_2.png) [@casterQ](https://discuss.elastic.co/u/casterQ)
#### Post date: [March 22, 2022, 3:21am UTC](https://discuss.elastic.co/t/cant-sum-bucket-after-composite/300184/5 "2022-03-22T03:21:30Z")

</div>

is this meet you need？

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "parentId.keyword": "aaaa"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "username.keyword": "test"
          }
        }
      ]
    }
  },
  "aggs": {
    "summary_total_price": {
      "sum": {
        "field": "summary.total.price"
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [April 19, 2022, 3:22am UTC](https://discuss.elastic.co/t/cant-sum-bucket-after-composite/300184/6 "2022-04-19T03:22:21Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
