title: ElasticSearch: groovy script to painless
body: <p>I wrote this script using groovy language (<code>datediff.groovy</code>):</p>
  
  <pre><code>(doc[secondDateField].date.millis - doc[firstDateField].date.millis) / 3600000
  </code></pre>
  
  <p>It works fine. Nevertheless, I'm intended to move these script to <code>painless</code> (<code>datediff.painless</code>). Nevertheless, I'm getting compilation error on straup:</p>
  
  <pre><code>[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"
  }
  </code></pre>
  
  <p>It's telling me <code>secondDateField</code> is not defined. Could you help me in order to get it?</p>
  
  <p><code>secondDateField</code> is not the field named <code>secondDateField</code>. It's a script parameter (like <code>firstDateField</code>), and doc[parameter] should access to the field set in the <code>parameter</code> content. If <code>secondDateField = "fieldOne"</code>, <code>doc[secondDateField] =&gt; doc["fieldOne"]</code>...</p>