4

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?

Jonah
  • 9,535
  • 5
  • 39
  • 74
  • Hi Jonah , If you don't mind could you please let me know how the above script is helping to boost most recent items ..? Thanks for your help . ( I am just doing order by "updated date" descending to get the latest items . This is a date filed with time stamp . ) .. Thanks again – Bujji Jul 06 '17 at 15:34
  • Not sure exactly what info you're needing, but if you check my answer below, you can see the entire usage context. – Jonah Jul 08 '17 at 00:26
  • Thank you for your response and I am sorry I did miss your comment . In your question you mentioned the below lines . I am sorry I couldn't find the reason behind using this script to boost performance . Why can not we just directly do sort by date to get the latest items , why do we have to use this 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" – Bujji Jul 12 '17 at 14:17
  • Your lines ""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"" – Bujji Jul 12 '17 at 14:18
  • @Bujji The reason I use this script is to _gently boost_ results by how recent they are. If I just sorted by date, they would no-longer be sorted by relevance to the search query. – Jonah Jul 19 '17 at 17:41
  • Thank you for your response . This is really helpful – Bujji Jul 20 '17 at 13:04

2 Answers2

4

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

Emil Stenström
  • 10,369
  • 8
  • 45
  • 70
  • Even though I already solved the problem, I'm marking yours as the answer since it is the most direct solution to the question I asked. Thanks! – Jonah Jan 08 '15 at 18:35
2

I didn't find out how to get the time within the script, but it seems you can add variables to the query that are available within the script. Here's the resulting 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"
            }
        }
    ],
}

And my application generates the timestamp (in this case 1409001061000).

Jonah
  • 9,535
  • 5
  • 39
  • 74