Format Scripted metrics result

Hi everyone,

I'm tring to format the result of my scripted metric from seconds to hh:mm:ss
I didn't find any easy way to perform that and i've tried using the reduce script to do so.
Doing this i've got a surprising error telling me ""unexpected token [';'] was expecting one of [].""
the semi-colon mentionned in this error is the one in the reduce script just after the for.

here is the request i used

POST elasticube-1101_homolog-2017.05.15/_search
"aggs": {
"profit": {
"scripted_metric": {
"init_script" :
"params._agg.numerator = []; params._agg.denominator = [];"
"map_script" :
"params._agg.numerator.add(doc["action.cp"].value); params._agg.denominator.add(doc["action.can"].value + doc["action.cap"].value);"
"combine_script" : "int num_sum=0; int den_sum=0; for (t in params._agg.numerator) { num_sum+= t} for (t in params._agg.denominator) { den_sum += t } return ['num_sum': num_sum, 'den_sum': den_sum];",
"reduce_script" : "float num_sum = 0;for (a in params._aggs) {num_sum += a.num_sum;};var h = Math.floor(num_sum / 3600);var m = Math.floor(num_sum % 3600 / 60);var s = Math.floor(num_sum % 3600 % 60);return h + ":" + m + ":" + s;"

Can someone help me with formatting my result?

I think the problem is the semicolon after the curly brace:

Formatted, that looks like:

for (a in params.aggs) {
  numsum += a.num_sum;
}; // <-- here

Thanks Polyfractal for your answer but the semicolon wasn't the problem.
I solved my issue by changing the type of my variables from int to double.
I deal with a large ammount of milliseconds that just doesn't fit into int

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