Returning a map from scripted metric aggregation

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.

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.

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}}"}

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