Search code examples
groovyelasticsearchtimestampmvel

ElasticSearch Get Time in Groovy Script


My application is using this script for boosting more recent items in the index:

(5 / ((3.16*pow(10,-11)) * abs(time() - doc[\'date\'].date.getMillis()) + 0.2)) + 1.0

It's written in MVEL, but as of 1.3, MVEL is deprecated for Groovy. The script throws this error now:

GroovyScriptExecutionException[MissingMethodException[No signature of method: Script4.time() is applicable for argument types: () values: []\nPossible solutions: find(), dump(), find(groovy.lang.Closure), use([Ljava.lang.Object;), is(java.lang.Object), with(groovy.lang.Closure)]]

This sounds to me like the function for getting a millisecond timestamp is different in Groovy. I tried System.currentTimeMillis(), but it gave another error saying it didn't support imports.

So how can I fix the time() function to work with Groovy?


Solution

  • As you already have discovered, you need to rewrite your script in Groovy instead of MVEL. Instead of time you need to use DateTime.now().getMillis(). Here's an example of how you use it: http://writequit.org/org/es/index.html#time-in-groovy-script