# Terms Aggregation: auto-number the buckets in a sub-aggregation with script

**URL:** https://discuss.elastic.co/t/terms-aggregation-auto-number-the-buckets-in-a-sub-aggregation-with-script/27952
**Category:** Elasticsearch
**Created:** [August 24, 2015, 1:26pm UTC](https://discuss.elastic.co/t/terms-aggregation-auto-number-the-buckets-in-a-sub-aggregation-with-script/27952 "2015-08-24T13:26:52Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![drjz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/drjz/32/972_2.png) [@drjz](https://discuss.elastic.co/u/drjz)
#### Post date: [August 24, 2015, 1:26pm UTC](https://discuss.elastic.co/t/terms-aggregation-auto-number-the-buckets-in-a-sub-aggregation-with-script/27952/1 "2015-08-24T13:26:52Z")

</div>

I am wondering whether it is possible to use a sub-aggregation to automatically number the buckets retrieved. Basically, I want to incrementally count the buckets.

So:

```
{
"key" : "8130837",
"doc_count" : 1,
"bucketOrder" : {
  "value" : 1.0
 }
}, {
"key" : "8131106",
"doc_count" : 1,
"bucketOrder" : {
  "value" : 2.0
 }
}, {
"key" : "8131131",
"doc_count" : 1,
"bucketOrder" : {
  "value" : 3.0
 }
}

```

The reason why I want this incremental count, is so that I can use this in a script filter.

---

<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: [July 5, 2017, 11:54pm UTC](https://discuss.elastic.co/t/terms-aggregation-auto-number-the-buckets-in-a-sub-aggregation-with-script/27952/2 "2017-07-05T23:54:12Z")

</div>


