# Returning a map from scripted metric aggregation

**URL:** <https://discuss.elastic.co/t/returning-a-map-from-scripted-metric-aggregation/98556>\
**Category:** Elasticsearch\
**Created:** [August 28, 2017, 11:29am UTC](https://discuss.elastic.co/t/returning-a-map-from-scripted-metric-aggregation/98556 "2017-08-28T11:29:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashish\_Goel](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Ashish\_Goel](https://discuss.elastic.co/u/Ashish_Goel)\
**Post date:** [August 28, 2017, 11:29am UTC](https://discuss.elastic.co/t/returning-a-map-from-scripted-metric-aggregation/98556/1 "2017-08-28T11:29:22Z")

</div>

Hi there,

I have been using ElasticSearch 1.5 before. And in that, there was a scripted metric aggregation with groovy as the language where I was returning a map.

Now, we are working on upgrading to ElasticSearch 5.4 and have converted the same aggregation to Painless. But I am having a problem with the reduce script step. When trying to return a map as the output of the reduce step, it gives a class cast exception.

I also tried returning the string value of the map from the reduce script, in the hopes that I will convert that string to a json object and then to JAVA Map. But the format of the returned string was something like:  
`{616137804988881539={skips=0, notAnswered=0}, 686788170415649923={skips=0, notAnswered=0}}`  
instead of the usual json format:  
`{616137804988881539:{skips:0, notAnswered:0}, 686788170415649923:{skips:0, notAnswered:0}}`  
Because of this, json conversion fails.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 28, 2017, 6:35pm UTC](https://discuss.elastic.co/t/returning-a-map-from-scripted-metric-aggregation/98556/2 "2017-08-28T18:35:46Z")

</div>

Can you provide the actual error you get. You say you get a class cast exception, but the actual exception (with stack trace preferably) would be necessary to figure this out.

---

<div class="post-metadata">

**Author:** ![Ashish\_Goel](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Ashish\_Goel](https://discuss.elastic.co/u/Ashish_Goel)\
**Post date:** [August 28, 2017, 7:15pm UTC](https://discuss.elastic.co/t/returning-a-map-from-scripted-metric-aggregation/98556/3 "2017-08-28T19:15:25Z")

</div>

I have reproduced the same behaviour with simple scripts using ES 5.4.3, stated below:

init\_script: `params._agg.result = [:]`

map\_script: `if (!params._agg.result.containsKey("total")) {params._agg.result = ["total":1]} else {params._agg.result.total += 1}`

reduce\_script: `Map finalResult = [:]; int i = 0; for (Map agg : params._aggs) { Map result = agg.result; finalResult[i++] = result;} return finalResult;`

This gives the following output:  
`{"msg":"[class_cast_exception] null","path":"redacted/_search","query":{},"body":"{"query":{"match_all":{}},"aggs":{"geSumMetric":{"scripted_metric":{"init_script":{"lang":"painless","inline":"params._agg.result = [:]"},"map_script":{"lang":"painless","inline":"if (!params._agg.result.containsKey(\\"total\\")) {params._agg.result = [\\"total\\":1]} else {params._agg.result.total += 1}"},"reduce_script":{"lang":"painless","inline":"Map finalResult = [:]; int i = 0; for (Map agg : params._aggs) { Map result = agg.result; finalResult[i++] = result;} return finalResult;"}}}}}","statusCode":500,"response":"{"error":{"root_cause":[{"type":"class_cast_exception","reason":null}],"type":"class_cast_exception","reason":null},"status":500}"}`

This is the entire result from the call, no detailed stack trace was returned for this error.

If I return `finalResult.toString()` in the reduce\_script, then I get: `{"value":"{0={total=65}}"}`

---

<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:** [September 25, 2017, 7:15pm UTC](https://discuss.elastic.co/t/returning-a-map-from-scripted-metric-aggregation/98556/4 "2017-09-25T19:15:26Z")

</div>

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