title: Elastic Search: How to write multi statement scripts?
body: <p>I have values stored on a document in an Elasticsearch index.<br>
  I need to do some date manipulation on the values and return a boolean value to be used in a filter.<br>
  The script covers several lines, and I can't get it to run.</p>
  
  <p>I've written other single scripts that work fine, however I know less than nothing about Groovy and very little about Elastic search.</p>
  
  <p>Each and every sample I can find with a script has one line and only one line.</p>
  
  <p>So basically how would I take this perfectly valid script</p>
  
  <pre><code>"script": {
      "script": "doc['state'].value == 'completed' &amp;&amp; doc['lastStateUpdate'].value &lt; doc['dueDate'].value"
      }
  </code></pre>
  
  <p>And turn it into some thing like </p>
  
  <pre><code>"script": {
      "script": "def isCompleted = doc['state'].value == 'completed' 
                 def preSLA = doc['lastStateUpdate'].value &lt; doc['dueDate'].value
                 return isCompleted &amp;&amp; preSLA"
      }
  </code></pre>
  
  <p>I'm not mad about the idea of creating a write-only one liner that expresses the logic, I can see more of these coming down the line and while this one is relatively straight-forward, a "one liner" isn't going to cut it.</p>
  
  <p>The alternative here is to do some preprocessing on the document before it's indexed, and add extra data to it. However this has drawbacks in that it's rather inflexible and we'd need to reindex all the data to change these aggregations, which we'd rather not do.</p>