title: elasticsearch: running a script over top hits aggregation fields
body: <p>I want to perform a terms aggregation and in every results bucket get the hit fields after a script manipulation was performed.</p>
  
  <p>For example, if these are the documents:</p>
  
  <pre><code>{"age": 15, "firstName": "Dana", "lastName": "Miller"}
  {"age": 15, "firstName": "Michelle", "lastName": "Bob"}
  {"age": 32, "firstName": "Mary", "lastName": "Smith"}
  {"age": 32, "firstName": "Anna", "lastName": "Taylor"}
  </code></pre>
  
  <p>The aggregation is by the "age" field, and the script is:
  "return 'doc['firstName'] + ' ' + doc['lastName']"</p>
  
  <p>The results should be:</p>
  
  <p>bucket 1 (age: 15): </p>
  
  <ul>
  <li>"Dana Miller"</li>
  <li>"Michelle Bob"</li>
  </ul>
  
  <p>Buchet 2 (age 32):</p>
  
  <ul>
  <li>Mary Smith</li>
  <li>Anna Taylor</li>
  </ul>
  
  <p>Is this possible in elasticsearch?</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>In addition, I'm looking for a way to run a script over multiple hits in a bucket.
  For example, if we use the documents above and the terms aggregation by the age field, can I get the results of the hits in the bucket together in the following way?</p>
  
  <p>Bucket 1 (age 15):</p>
  
  <ul>
  <li>"Dana Miller and Michelle Bob"</li>
  </ul>
  
  <p>Bucket2 (age 32):</p>
  
  <ul>
  <li>"Mary Smith and Anna Taylor"</li>
  </ul>
  
  <p>Is it possible in ES?</p>
  
  <p>thank you.</p>