且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ElasticSearch获取Groovy脚本中的时间

更新时间:2023-12-05 16:58:40

p>如您已经发现的,您需要在Groovy中重写脚本,而不是MVEL。而不是时间,您需要使用 DateTime.now()。getMillis()。以下是您使用它的示例: http:// writequit。 org / org / es / index.html#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?

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