# Map Object in scripted metric aggregation

**URL:** <https://discuss.elastic.co/t/map-object-in-scripted-metric-aggregation/136780>\
**Category:** Elasticsearch\
**Created:** [June 21, 2018, 2:28am UTC](https://discuss.elastic.co/t/map-object-in-scripted-metric-aggregation/136780 "2018-06-21T02:28:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Varun\_Somani](https://avatars.discourse-cdn.com/v4/letter/v/ecccb3/32.png) [@Varun\_Somani](https://discuss.elastic.co/u/Varun_Somani)\
**Post date:** [June 21, 2018, 2:28am UTC](https://discuss.elastic.co/t/map-object-in-scripted-metric-aggregation/136780/1 "2018-06-21T02:28:57Z")

</div>

I have the following Reduce Script, which is working fine when I do the query. When I am trying to call the same via Java I get am not able to call "getScriptedMetricAggregation", which is giving me Error when it is trying to convert the object to double. (Which I expect) but then how can I get the response map?

Reduce Script:

```
{
  "script" : {
    "lang": "painless",
    "source" : "Map result = new HashMap(); double words = 0, error = 0; for (a in params._aggs) { words += a['words'] ; error += a['error']; } result.put('words', words); result.put('error', error); return result"
  }
}

```

Java Code (Since it is nested on top of Term aggregation only putting the significant code):

```
    for (TermsAggregation.Entry entry:termsAggregation.getBuckets()) {
        log.info("key = {},{}", entry.getKey(), entry);
        ScriptedMetricAggregation scriptedResult = entry.getAggregation("scripted_error", ScriptedMetricAggregation.class);
        System.out.println(scriptedResult);
    }

```

Error On Screen:

```
Caused by: java.lang.UnsupportedOperationException: JsonObject
	at com.google.gson.JsonElement.getAsDouble(JsonElement.java:204)
	at io.searchbox.core.search.aggregation.SingleValueAggregation.<init>(SingleValueAggregation.java:19)
	at io.searchbox.core.search.aggregation.ScriptedMetricAggregation.<init>(ScriptedMetricAggregation.java:15)
	... 3 more

```

How to proceed ?  
To me this seems like a limitation in Java-API.

---

<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 19, 2018, 2:32am UTC](https://discuss.elastic.co/t/map-object-in-scripted-metric-aggregation/136780/2 "2018-07-19T02:32:15Z")

</div>

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