# "bucket\_script" aggregation initialization exception

**URL:** <https://discuss.elastic.co/t/bucket-script-aggregation-initialization-exception/205381>\
**Category:** Elasticsearch\
**Created:** [October 27, 2019, 11:14am UTC](https://discuss.elastic.co/t/bucket-script-aggregation-initialization-exception/205381 "2019-10-27T11:14:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohammad\_Mousavi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohammad_mousavi/32/56508_2.png) [@Mohammad\_Mousavi](https://discuss.elastic.co/u/Mohammad_Mousavi)\
**Post date:** [October 27, 2019, 11:14am UTC](https://discuss.elastic.co/t/bucket-script-aggregation-initialization-exception/205381/1 "2019-10-27T11:14:50Z")

</div>

Hi, This is my query:  
{  
"aggs": {  
"users": {  
"cardinality": {  
"field": "user\_id.keyword"  
},  
"aggs": {  
"time": {  
"sum": {  
"field": "ts\_duration"  
}  
},  
"avg\_time": {  
"bucket\_script": {  
"buckets\_path": { "totaluser": "users", "totaltime": "time"},  
"script": "totaltime / totaluser"  
}}}}}}

and I got this error:

{  
"error": {  
"root\_cause": [  
{  
"type": "aggregation\_initialization\_exception",  
"reason": "Aggregator [users] of type [cardinality] cannot accept sub-aggregations"  
}  
],  
"type": "aggregation\_initialization\_exception",  
"reason": "Aggregator [users] of type [cardinality] cannot accept sub-aggregations"  
},  
"status": 500  
}  
What that's means ? Thank you

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 27, 2019, 1:35pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-initialization-exception/205381/2 "2019-10-27T13:35:49Z")

</div>

There are two problems with your request. For starters, you have nested the three aggregations inside each other. And as the error already suggests: a `cardinality` aggregation cannot accept sub-aggregations. All three aggregations need to be at the same level.

Furthermore, the `bucket_script` aggregation can only be nested inside a multi-bucket aggregation. That may change in the future (you can follow the work on [this Github issue](https://github.com/elastic/elasticsearch/issues/14600)), but for now you have to use a workaround. You can nest the `bucket_script` aggregation inside a `filters` aggregation that only has one filter, with a `match_all` query. It's not pretty, but it gets the work done:

```auto
{
  "size": 0,
  "aggs": {
    "all_data": {
      "filters": {
        "filters": {
          "all": {
            "match_all": {}
          }
        }
      },
      "aggs": {
        "users": {
          "cardinality": {
            "field": "user_id.keyword"
          }
        },
        "time": {
          "sum": {
            "field": "ts_duration"
          }
        },
        "avg_time": {
          "bucket_script": {
            "buckets_path": {
              "totaluser": "users",
              "totaltime": "time"
            },
            "script": "params.totaltime / params.totaluser"
          }
        }
      }
    }
  }
}

```

(By the way, please format any code snippets on this forum using the `</>` button. It makes your post much easier to read and it's more likely that someone will help you that way 🙂 )

---

<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:** [November 24, 2019, 1:35pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-initialization-exception/205381/3 "2019-11-24T13:35:51Z")

</div>

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