# Bucket\_script error while using values from aggregation

**URL:** https://discuss.elastic.co/t/bucket-script-error-while-using-values-from-aggregation/89119
**Category:** Elasticsearch
**Created:** [June 12, 2017, 10:08pm UTC](https://discuss.elastic.co/t/bucket-script-error-while-using-values-from-aggregation/89119 "2017-06-12T22:08:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![elango](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@elango](https://discuss.elastic.co/u/elango)
#### Post date: [June 12, 2017, 10:08pm UTC](https://discuss.elastic.co/t/bucket-script-error-while-using-values-from-aggregation/89119/1 "2017-06-12T22:08:33Z")

</div>

I am trying to calculate the difference between 2 aggregate values using bucket\_script, but getting class\_cast\_exception error.

Here I am getting the current max connection count (now\_cnx\_count) and the average of past 4 weeks values (avg\_previous\_week). Now I using bucket\_script to calculate the difference between 2 values.

Below is the query I am using:

{  
"size": 0,  
"aggs": {  
"active\_nbr" : {  
"filter" : {  
"term" : {  
"\_type" : "mytype"  
}  
},  
"aggs": {  
"now\_cnx\_count": {  
"date\_range": {  
"field": "@timestamp",  
"ranges": [  
{  
"from": "now-15m",  
"to": "now"  
}  
]  
},  
"aggs": {  
"n\_cnx\_count": {  
"max": {  
"field": "Cnx\_Count"  
}  
}  
}  
},  
"previous\_week\_cnx\_count": {  
"date\_range": {  
"field": "@timestamp",  
"ranges": [  
{  
"from": "now-7d-15m",  
"to": "now-7d"  
},  
{  
"from": "now-14d-15m",  
"to": "now-14d"  
},  
{  
"from": "now-21d-15m",  
"to": "now-21d"  
},  
{  
"from": "now-28d-15m",  
"to": "now-28d"  
}  
]  
},  
"aggs": {  
"p\_cnx\_count": {  
"max": {  
"field": "Cnx\_Count"  
}  
}  
}  
},  
"avg\_previous\_week": {  
"avg\_bucket": {  
"buckets\_path": "previous\_week\_cnx\_count\>p\_cnx\_count"  
}  
},  
"nowvspast\_diff": {  
"bucket\_script": {  
"buckets\_path": {  
"now\_val": "now\_cnx\_count\>n\_cnx\_count",  
"past\_val": "avg\_previous\_week"  
},  
"script": "params.now\_val / params.past\_val \* 100"  
}  
}

```
  }
}

```

}  
}

Error:

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

Can someone help me with this?

---

<div class="post-metadata">

### Author: ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)
#### Post date: [June 20, 2017, 9:02am UTC](https://discuss.elastic.co/t/bucket-script-error-while-using-values-from-aggregation/89119/2 "2017-06-20T09:02:08Z")

</div>

You cannot reference a multi-bucket aggregation in a `buckets_path`. In your case your `now_val` path is trying to reference an aggregation inside a date\_range aggregation. Instead you should use a `filter` aggregation containing a `range` filter and use that for your `bucket_path`

---

<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 18, 2017, 9:02am UTC](https://discuss.elastic.co/t/bucket-script-error-while-using-values-from-aggregation/89119/3 "2017-07-18T09:02:22Z")

</div>

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