ES watcher Transform: formatting the script source

Hi team,

I'm really getting a hard time working with transform scripts cause it's only allowing me to write on 1 line:

example:

"transform": {
"script": {
"source": " String k; int i; int s; int a; float sar; for(i=0;i<ctx.payload.aggregations.direction.buckets.size();i++){if(ctx.payload.aggregations.direction.buckets[i].key == 'submitted'){s=ctx.payload.aggregations.direction.buckets[i].doc_count;} else {a=ctx.payload.aggregations.direction.buckets[i].doc_count;}} sar=a*100/s;DecimalFormat df = new DecimalFormat('###'); k=df.format(sar); List Result = [ 'Attempted', a, 'Submitted', s, k ]; return Result;",
"lang": "painless"
}
}

=> question : is it possible to have a Carriage return on the script to have something like the following:

"transform": {
"script": {
"source": "
String k;
int i;
int s;
int a;
float sar;
for(i=0;i<ctx.payload.aggregations.direction.buckets.size();i++)
{
if(ctx.payload.aggregations.direction.buckets[i].key == 'submitted')
{
s=ctx.payload.aggregations.direction.buckets[i].doc_count;
}
else
{
a=ctx.payload.aggregations.direction.buckets[i].doc_count;
}
}
sar=a*100/s;
DecimalFormat df = new DecimalFormat('###');
k=df.format(sar);
List Result = [ 'Attempted', a, 'Submitted', s, k ];
return Result;",
"lang": "painless"
}
}

I put """ surrounding the source block.. but saying "Invalid json".

is there a way to do it or put the script in a file and call it??

Thanks

The usage of triple ticks within kibana looks correct at a first glance. Is this Kibana and if so, which version of the stack are you using?

--Alex

Yes Alex, this is Kibana, v 6.8.0

can you share the full watch as text, so one can try and copy paste that into a dev-tools console?

1 Like

Hi Alex,

thanks for the hint. Actually I was using the advanced watcher console and it wasn't working from there.
Now I put my watch in dev-tools console and from there I was able to execute it using the triple ticks.

thanks for you support

1 Like

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