Can't sum bucket after composite

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

This is my query

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

{
  "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

{
  "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

The composite agg is not currently compatible with pipeline aggregations, please read this doc:

1 Like

Are there another way for summary data without using aggregations?

What's the difference between the two?

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

is this meet you need?

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

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