Sum of a field after terms aggregation

Hi @Miguel_Azorin

Try use sum buckets:

{
  "size": 0,
  "aggs": {
    "group_by_ID": {
      "terms": {
        "field": "id",
        "min_doc_count": 1
      },
      "aggs": {
        "maxValue": {
          "max": {
            "field": "numValue"
          }
        }
      }
    },
    "total": {
      "sum_bucket": {
        "buckets_path": "group_by_ID>maxValue"
      }
    }
  }
}
1 Like