# Pipeline aggregation throwing an error

**URL:** <https://discuss.elastic.co/t/pipeline-aggregation-throwing-an-error/296839>\
**Category:** Elasticsearch\
**Created:** [February 10, 2022, 12:45pm UTC](https://discuss.elastic.co/t/pipeline-aggregation-throwing-an-error/296839 "2022-02-10T12:45:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![saurab](https://avatars.discourse-cdn.com/v4/letter/s/e274bd/32.png) [@saurab](https://discuss.elastic.co/u/saurab)\
**Post date:** [February 10, 2022, 12:45pm UTC](https://discuss.elastic.co/t/pipeline-aggregation-throwing-an-error/296839/1 "2022-02-10T12:45:45Z")

</div>

I am working with the kibana\_sample\_data\_ecommerce index. I will display two queries here:

```auto
GET kibana_sample_data_ecommerce/_search?size=0
{
  "aggs": {
    "customer_centric": {
      "terms": {
        "field": "customer_full_name.keyword", 
        "size": 5000
      },
      "aggs": {
        "myagg": {
          "terms": {
            "field": "category.keyword"
          }, 
          "aggs": {
            "taxful_total_price_for_this_category": {
              "sum": {
                "field": "taxful_total_price"
              }
            }, 
            "number_of_orders_for_this_category": {
              "cardinality": {
                "field": "order_id"
              }
            }
          }
        }
      }
    }
  }
}

```

The result is:

```auto
"aggregations" : {
    "customer_centric" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Elyssa Hansen",
          "doc_count" : 7,
          "myagg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "Women's Clothing",
                "doc_count" : 5,
                "taxful_total_price_for_this_category" : {
                  "value" : 234.84375
                },
                "number_of_orders_for_this_category" : {
                  "value" : 5
                }
              },
              {
                "key" : "Women's Accessories",
                "doc_count" : 4,
                "taxful_total_price_for_this_category" : {
                  "value" : 280.9375
                },
                "number_of_orders_for_this_category" : {
                  "value" : 4
                }
              },
              {
                "key" : "Women's Shoes",
                "doc_count" : 4,
                "taxful_total_price_for_this_category" : {
                  "value" : 284.921875
                },
                "number_of_orders_for_this_category" : {
                  "value" : 4
                }
              }
            ]
          }
        },

```

In addition to calculating the number of orders for each category for each customer, I also want to calculate the total number of orders for each customer. So I ran this query:

```auto
GET kibana_sample_data_ecommerce/_search?size=0
{
  "aggs": {
    "customer_centric": {
      "terms": {
        "field": "customer_full_name.keyword", 
        "size": 5000
      }, 
      "aggs": {
        "total_number_of_orders": {
          "cardinality": {
            "field": "order_id"
          }
        }
      }, 
      "aggs": {
        "myagg": {
          "terms": {
            "field": "category.keyword"
          }, 
          "aggs": {
            "taxful_total_price_for_this_category": {
              "sum": {
                "field": "taxful_total_price"
              }
            }, 
            "number_of_orders_for_this_category": {
              "cardinality": {
                "field": "order_id"
              }
            }
          }
        }
      }
    }
  }
}

```

This gave me a compile-time error as follows:

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "json_parse_exception",
        "reason" : "Duplicate field 'aggs'\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 15, column: 13]"
      }
    ],
    "type" : "json_parse_exception",
    "reason" : "Duplicate field 'aggs'\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 15, column: 13]"
  },
  "status" : 400
}

```

It seems that you cannot have two sub-aggregations at the same level for a parent aggregation. How do I modify this query for it to work ? Please help! Thanks in advance.

---

<div class="post-metadata">

**Author:** ![asupranovich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asupranovich/32/101652_2.png) [@asupranovich](https://discuss.elastic.co/u/asupranovich)\
**Post date:** [February 10, 2022, 1:16pm UTC](https://discuss.elastic.co/t/pipeline-aggregation-throwing-an-error/296839/2 "2022-02-10T13:16:07Z")

</div>

Hi @saurab  
Try this query:

```auto
GET kibana_sample_data_ecommerce/_search?size=0
{
  "aggs": {
    "customer_centric": {
      "terms": {
        "field": "customer_full_name.keyword", 
        "size": 5000
      }, 
      "aggs": {
        "total_number_of_orders": {
          "cardinality": {
            "field": "order_id"
          }
        },
        "myagg": {
          "terms": {
            "field": "category.keyword"
          }, 
          "aggs": {
            "taxful_total_price_for_this_category": {
              "sum": {
                "field": "taxful_total_price"
              }
            }, 
            "number_of_orders_for_this_category": {
              "cardinality": {
                "field": "order_id"
              }
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![saurab](https://avatars.discourse-cdn.com/v4/letter/s/e274bd/32.png) [@saurab](https://discuss.elastic.co/u/saurab)\
**Post date:** [February 11, 2022, 6:57am UTC](https://discuss.elastic.co/t/pipeline-aggregation-throwing-an-error/296839/3 "2022-02-11T06:57:50Z")

</div>

I found out the difficulty. you need to use a sum\_bucket aggregation. Here is what works:

```auto
GET kibana_sample_data_ecommerce/_search?size=0
{
  "aggs": {
    "customer_centric": {
      "terms": {
        "field": "customer_full_name.keyword", 
        "size": 5000
      }, 
      "aggs": {
        "unique_categories": {
          "terms": {
            "field": "category.keyword"
          }, 
          "aggs": {
            "number_of_orders_for_this_category": {
              "cardinality": {
                "field": "order_id"
              }
            }
          }
        },
        "total_number_of_orders": {
          "sum_bucket": {
            "buckets_path": "unique_categories>number_of_orders_for_this_category"
          }
        }
      }
    }
  }
}

```

Thanks for the effort.

---

<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:** [March 11, 2022, 6:57am UTC](https://discuss.elastic.co/t/pipeline-aggregation-throwing-an-error/296839/4 "2022-03-11T06:57:54Z")

</div>

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