Script quering with Painless scripts

Hi!

Have tried a lot of things, like Java Instant.now().toEpochMilli() or joda Days().toStandardDuration() with no success.

Found out what while Java Instant class isn't accepted, Java System.currentTimeMillis() is.

My last try was:

"script":{
    "script":{
        "source":"doc['activated'].value.addDays(doc['duration']).getMillis() > System.currentTimeMillis()",
        "lang":"painless"
    }
}

But it fails with:

            "reason": {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                    "doc['activated'].value.addDays(doc['duration']).getMillis() > System.currentTimeMillis()",
                    "                                  ^---- HERE"
                ],
                "script": "doc['activated'].value.addDays(doc['duration']).getMillis() > System.currentTimeMillis()",
                "lang": "painless",
                "caused_by": {
                    "type": "illegal_argument_exception",
                    "reason": "Unable to find dynamic method [addDays] with [1] arguments for class [org.joda.time.MutableDateTime]."
                }
            }

Here joda .getMillis() method is found and works as expected, while joda .addDays() method isn't found.

Feel so confused which Java and joda classes and methods are accepted with Script Queries, and how to accomplish my task.

Ok, got it to work with:

"script":{
	"script":{
		"source":"doc['activated'].value.getMillis() + doc['duration'].value*24*60*60*1000 > System.currentTimeMillis()",
		"lang":"painless"
	}
}

But it's not a nice solution.

And while scripting docs refers to Java SE 8 Language Specification, Java TimeUnit.DAYS.toMillis() is not available for usage with "reason": "Variable [TimeUnit] is not defined.". And there is a lot of similar situations: some methods can be used, some - can't.

Is there an explanation for that? Am I missing something? Did I misunderstood Java classes and methods usage?

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