title: Looping through hits using painless script
body: <p>Is there a way In Elasticsearch 5.3 to loop through query results using painless script to compute difference between the hits returned by query.</p>
  
  <p>I was trying to loop through the results in a for loop in script and could not what entity to loop on, i was hopping that scripts have something like the update api has ctx.payload.hits.hits that can be used in for loop.</p>
  
  <pre><code>"script": {
          "lang": "painless",
          "inline": "int total = 0; for (int i = 0; i &lt;  ??? ; i++) { total +=  ???[i]._source['age'].value; } return total;"
      }
  </code></pre>