# BucketPath to ScriptedMetric not working?

**URL:** https://discuss.elastic.co/t/bucketpath-to-scriptedmetric-not-working/109904
**Category:** Elasticsearch
**Created:** [December 1, 2017, 9:52am UTC](https://discuss.elastic.co/t/bucketpath-to-scriptedmetric-not-working/109904 "2017-12-01T09:52:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![SparkyRih](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sparkyrih/32/24762_2.png) [@SparkyRih](https://discuss.elastic.co/u/SparkyRih)
#### Post date: [December 1, 2017, 9:52am UTC](https://discuss.elastic.co/t/bucketpath-to-scriptedmetric-not-working/109904/1 "2017-12-01T09:52:00Z")

</div>

I'm trying to get a ScriptedMetric in a BucketScript, that should be possible right?

This is my query (in NEST)  
.ScriptedMetric("Urgency-Level", sm =\> sm  
.InitScript(i =\> i.Inline("params.\_agg.data = []").Lang("painless"))  
.MapScript(i =\> i.Inline("params.\_agg.data.add(doc.urgency.value)").Lang("painless"))  
.CombineScript(i =\> i.Inline("int urgency = 0; for (u in params.\_agg.data) { urgency += u } return urgency").Lang("painless"))  
.ReduceScript(i =\>i.Inline("int urgency = 0; for (a in params.\_aggs) { urgency += a } return urgency").Lang("painless"))  
)  
.BucketScript("finalScore", scb =\> scb  
.BucketsPath(bp =\> bp  
.Add("avgScore", "Avg-Score")  
.Add("urgencyLevel", "Urgency-Level")  
.Add("maxDateTime", "Max-DateTime"))  
.Script(i =\> i.Inline("params.avgScore").Lang("painless"))  
)

BTW, I do realize that the ScriptedMetric does exactly do what the Sum aggregation does, but that's going to change of course... The result of this metric is a nice single value 11...

But I'm getting the error: "aggregation\_execution\_exception Reason: "buckets\_path must reference either a number value or a single value numeric metric aggregation, got: org.elasticsearch.search.aggregations.metrics.scripted.InternalScriptedMetric"

As if the ScriptedMetric hasn't ran yet?

---

<div class="post-metadata">

### Author: ![SparkyRih](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sparkyrih/32/24762_2.png) [@SparkyRih](https://discuss.elastic.co/u/SparkyRih)
#### Post date: [December 7, 2017, 7:59am UTC](https://discuss.elastic.co/t/bucketpath-to-scriptedmetric-not-working/109904/2 "2017-12-07T07:59:46Z")

</div>

Anybody?

---

<div class="post-metadata">

### Author: ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)
#### Post date: [December 7, 2017, 9:39am UTC](https://discuss.elastic.co/t/bucketpath-to-scriptedmetric-not-working/109904/3 "2017-12-07T09:39:10Z")

</div>

The scripted metric can return values of different type:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html#\_allowed\_return\_types](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html#_allowed_return_types)  
In your example you return a number so if you want to access the value you need to specify the full path. In your example it is `Urgency-Level.value` (value refers to the metric computed by your reduce script).

---

<div class="post-metadata">

### Author: ![SparkyRih](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sparkyrih/32/24762_2.png) [@SparkyRih](https://discuss.elastic.co/u/SparkyRih)
#### Post date: [December 7, 2017, 10:36am UTC](https://discuss.elastic.co/t/bucketpath-to-scriptedmetric-not-working/109904/4 "2017-12-07T10:36:26Z")

</div>

You've got to be kidding me :S

I've tried everything, including this, but when I tried this I probably did something else wrong (like using groovy instead of painless)...

But it finally works now, thanks you!

---

<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: [January 4, 2018, 10:37am UTC](https://discuss.elastic.co/t/bucketpath-to-scriptedmetric-not-working/109904/5 "2018-01-04T10:37:01Z")

</div>

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