ElasticSearch Get Time In Groovy Script

My application uses this script to increase the number of last elements in the index:

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

It is written in MVEL, but with 1.3 MVEL is deprecated for Groovy. The script now raises this error:

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 a function for getting a millisecond timestamp in Groovy. I tried System.currentTimeMillis(), but he gave another error, saying that it does not support import.

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

+4
source share
2 answers

, script Groovy MVEL. DateTime.now().getMillis(). , : http://writequit.org/org/es/index.html#time-in-groovy-script

+4

, script, , , , script. JSON.

"function_score": {
    "query": /*...*/,
    "functions": [
        {
            "filter": {
                "exists": {
                    "field": "date"
                }
            },
            "script_score": {
                "params": {
                    "now": 1409001061000
                },
                "script": "(5 / ((3.16*pow(10,-11)) * abs(now - doc['date'].date.getMillis()) + 0.2)) + 1.0"
            }
        }
    ],
}

( 1409001061000).

+2

All Articles