Why my script transform failed with "No such property: org for class: Script8"

curl -XGET 'http://localhost:9200/.watch_history*/_search?pretty' -d '{ "sort" : [ { "result.execution_time" : "desc" } ]}':

            "condition": {
                "type": "script",
                "status": "success",
                "met": true
            },
            "transform": {
                "type": "script",
                "status": "failure",
                "reason": "GroovyScriptExecutionException[MissingPropertyException[No such property: org for class: Script8]]"
            }

I use Watcher Java API to build Transform:

watchSourceBuilder.transform(new ScriptTransform(new Script.Builder.DefaultType(scriptContent).build()));

Script code:

import org.elasticsearch.common.joda.time.DateTime
import org.elasticsearch.common.joda.time.Period
import org.elasticsearch.common.joda.time.format.DateTimeFormatter

String rangeString = '''PT1M''';
DateTimeFormatter fmt = org.joda.time.format.ISODateTimeFormat.dateTime();
DateTime scheduleTime = fmt.parseDateTime(ctx.trigger.triggered_time.toString());
Period period = org.joda.time.format.ISOPeriodFormat.standard().parsePeriod(rangeString);
DateTime  beginTime = scheduleTime.minus(period);
return [beginTime:beginTime, endTime:scheduleTime];

@spinscale Do you have any idea? :blush:

String rangeString = '''${range}''';
scheduleTime = org.joda.time.format.ISODateTimeFormat.dateTime().parseDateTime(ctx.trigger.triggered_time.toString());
period = org.joda.time.format.ISOPeriodFormat.standard().parsePeriod(rangeString);
beginTime = scheduleTime.minus(period);
return [beginTime:beginTime, endTime:scheduleTime];

I have tried that code, but it still gives:

"transform":{"type":"script","status":"failure","reason":"GroovyScriptExecutionException[MissingPropertyException[No such property: org for class: Script10]]"}

Hi Enniu,

Similar to the other Joda time classes, you need to use the Elasticsearch-shaded variants with the ISODateTimeFormat and ISOPeriodFormat. Change

org.joda.time.format.ISODateTimeFormat
org.joda.time.format.ISOPeriodFormat

to

org.elasticsearch.common.joda.time.format.ISODateTimeFormat
org.elasticsearch.common.joda.time.format.ISOPeriodFormat

then it should do what you expect.

Wow, that works!

But I feel strange, because @spinscale gave me such a transform:

"transform" : {
        "script" : "time = org.joda.time.format.ISODateTimeFormat.dateTime().withZone(org.joda.time.DateTimeZone.forID(\"Europe/Berlin\")).parseDateTime(ctx.trigger.triggered_time.toString()).toString() ; return [ time_as_string : time ]"
      } 

Why he use org.joda.time.format.ISODateTimeFormat there?

relate post:

I find I need original payload, so I change my return to:

return [origin_payload:ctx.payload, begin_time:beginTime, end_time:scheduleTime];

That works, but I wonder is there a way which will append new result to the payload rather than replace?

@Enniu_51 I tested with a 2.0.0-rc1 build, which needed a different import (because shading is gone, see this blog post from last week)

transform returns a new payload. So to shorten things a bit in the script you could also just add something like ctx.payload.foo = whatver ; return ctx.payload - but thats what you have to do.

--Alex

By return ctx.payload, then we will have some thing like ctx.payload.payload.whatever?