Groovy scripts to painless

I wrote this script using groovy language (datediff.groovy):

(doc[secondDateField].date.millis - doc[firstDateField].date.millis) / 3600000

It works fine. Nevertheless, I'm intended to move these script to painless (datediff.painless). Nevertheless, I'm getting compilation error on straup:

[esn1] failed to load/compile script [datediff]: {
   "type" : "script_exception",
   "reason" : "compile error",
   "caused_by" : {
     "type" : "illegal_argument_exception",
     "reason" : "Variable [secondDateField] is not defined."
   },
   "script_stack" : [
     "(doc[secondDateField].date.mil ...",
     "     ^---- HERE"
   ],
   "script" : "(doc[secondDateField].date.millis - doc[firstDateField].date.millis) / 3600000",
   "lang" : "painless"
}

It's telling me secondDateField is not defined. Could you help me in order to get it?

Try quotes around field names as per https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-painless.html#_accessing_doc_values_from_painless

seconfDateField is not a literal is a variable that acts as a script parameter.

The issue is how to get the content of this parameter in order to index the document.

params.secondDateField

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