# Is it possible to get the bucket interval of a histogram for bucket\_script?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-get-the-bucket-interval-of-a-histogram-for-bucket-script/133613>\
**Category:** Elasticsearch\
**Created:** [May 29, 2018, 5:38am UTC](https://discuss.elastic.co/t/is-it-possible-to-get-the-bucket-interval-of-a-histogram-for-bucket-script/133613 "2018-05-29T05:38:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![prismatic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prismatic/32/31683_2.png) [@prismatic](https://discuss.elastic.co/u/prismatic)\
**Post date:** [May 29, 2018, 5:38am UTC](https://discuss.elastic.co/t/is-it-possible-to-get-the-bucket-interval-of-a-histogram-for-bucket-script/133613/1 "2018-05-29T05:38:41Z")

</div>

I want to calculate (sum(my\_document\_field)/bucket\_interval) for all buckets in a date\_histogram. Its straightforward to calculate the sum with a sum\_bucket aggregation, however I'm having trouble figuring out how to get the interval of the given bucket to calculate the value I'm interested in. The interval specified for the date\_histogram may not be constant if 'week' or 'month', etc are used. Is there any way to calculate the interval for the parent bucket?

```
{
    "query": {
        ...
    },
    "aggregations": {
        "by_hour": {
            "date_histogram": {
                "field": "timestamp",
                "interval": "1h",
                "extended_bounds": {
                    "min": "now/d-5h",
                    "max": "now/d-1h"
                }
            },
            "aggregations": {
                "sum_raindrops": {
                    "sum": {
                        "field": "raindrops_seen"
                    }
                },
                "avg_scripts": {
                    "bucket_script": {
                        "buckets_path": {
                            "var_sum_raindrops": "sum_raindrops"
                        },
                        "script": "var_sum_raindrops / (?)"
                    }
                }
            }
        }
    }
}
```

---

<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:** [June 26, 2018, 5:38am UTC](https://discuss.elastic.co/t/is-it-possible-to-get-the-bucket-interval-of-a-histogram-for-bucket-script/133613/2 "2018-06-26T05:38:49Z")

</div>

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