# NPE when using both date\_range and bucket script aggregations

**URL:** <https://discuss.elastic.co/t/npe-when-using-both-date-range-and-bucket-script-aggregations/54257>\
**Category:** Elasticsearch\
**Created:** [June 29, 2016, 10:06am UTC](https://discuss.elastic.co/t/npe-when-using-both-date-range-and-bucket-script-aggregations/54257 "2016-06-29T10:06:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bennyvdhoogen](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@bennyvdhoogen](https://discuss.elastic.co/u/bennyvdhoogen)\
**Post date:** [June 29, 2016, 10:06am UTC](https://discuss.elastic.co/t/npe-when-using-both-date-range-and-bucket-script-aggregations/54257/1 "2016-06-29T10:06:47Z")

</div>

When trying to do calculations with two previously fetched/aggregated (using date\_range) fields I'm stumbling upon a Null Pointer Exception error whenever I use `bucket_script`. The strange thing is that the request below works perfectly fine when replacing `date_range` with `date_histogram` or a terms aggregation. The moment I fill in anything in the `script` field in my `bucket_script` I get a NPE as response.

The error message I'm getting as response:

> "{"error":{"root\_cause":,"type":"reduce\_search\_phase\_exception","reason":"[reduce] ","phase":"fetch","grouped":true,"failed\_shards":,"caused\_by":{"type":"null\_pointer\_exception","reason":null}},"status":503}"

My JSON request:

> ```
> {
> "query": {
> "bool": 
> "must": [
> {
> "query_string": {
> "default_field": "data.adgroup_id",
> "query": "(adgroup_id: 6044965376773 OR adgroup_id: 6045143401573 OR adgroup_id: 6044653493573 OR adgroup_id: 6044738299773)"
> }
> },
> {
> "term": {
> "agency_id": "215"
> }
> },
> {
> "range" : {
> "date" : {
> "gte" : "2016-06-01",
> "lte" : "2016-06-30"
> }
> }
> }
> ],
> "must_not": [],
> "should": []
> }
> },
> "from": 0,
> "size": 50,
> "sort": [],
> "aggs": {
> "indicators": {
> "date_range": {
> "field": "date",
> "format": "yyyy-MM-dd",
> "ranges": [
> { "from": "2016-06-01" }, 
> { "to": "2016-06-30" } 
> ]  
> },   
> "aggs": {
> "ind1_c_website_clicks": {
> "sum": {
> "field": "data.website_clicks"}
> },"ind1_c_impressions": {
> "sum": {
> "field": "data.impressions "}
> }
> 
> ,"ind1_ctr" : {
> "bucket_script": {
> "gap_policy" : "insert_zeros",
> "buckets_path": {
> "var_website_clicks" : "ind1_c_website_clicks",
> "var_impressions" : "ind1_c_impressions"
> },
> "script": "var_website_clicks / var_impressions * 100"
> } 
> } 
>                 
> }
> }
> }
> }
> 
> ```

If I leave out the `bucket_script` segment I get valid results for the fields `ind1_c_website_clicks` and `ind1_c_impressions` and I just want to do some direct calculations with those results. When changing the field names in `buckets_path` to gibberish I get some other error regarding ES being unable to find that field, which to me seems like it doesn't have anything to do with the path reference being incorrect. Putting ` 0` or `random()` in script to return a direct result also causes the NPE, so it doesn't seem to have anything to do with fields not being found or values being 0 / null.

I'm guessing it has something to do with the structure of the aggregations and the way date\_range accesses data. The pipeline aggregation doc provided some insights about parent and sibling buckets and I've tried moving `ind1_ctr` out of the aggregations {} since it seemed weird (I've copied this structuring according to one of the bucket script example) to me that this is placed at the same level as `ind1_c_impressions` which are to be aggregated prior to the execution of the script in `ind1_ctr`, but this led to some other errors.

If someone could point me into the right direction that would be great,

Thanks in advance.

---

<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, 10:39pm UTC](https://discuss.elastic.co/t/npe-when-using-both-date-range-and-bucket-script-aggregations/54257/2 "2017-07-05T22:39:39Z")

</div>


