title: Elastic Search return object with sum aggregation
body: <p>I am trying to get a list of the top 100 guests by revenue generated with Elastic Search. To do this I am using a <code>terms</code> and a <code>sum</code> aggregation. However it does return the correct values, I wan to return the entire <code>guest</code> object with the aggregation.</p>
  <p>This is my query:</p>
  <pre class="lang-json prettyprint-override"><code>GET reservations/_search
  {
    &quot;size&quot;: 0, 
    &quot;aggs&quot;: {
      &quot;top_revenue&quot;: {
        &quot;terms&quot;: {
          &quot;field&quot;: &quot;total&quot;,
          &quot;size&quot;: 100,
          &quot;order&quot;: {
            &quot;top_revenue_hits&quot;: &quot;desc&quot;
          }
        },
        &quot;aggs&quot;: {
          &quot;top_revenue_sum&quot;: {
            &quot;sum&quot;: {
              &quot;field&quot;: &quot;total&quot;
            }
          }
        }
      }
    }
  }
  </code></pre>
  <p>This returns a list of the top 100 guests but only the amount they spent:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;aggregations&quot; : {
      &quot;top_revenue&quot; : {
        &quot;doc_count_error_upper_bound&quot; : -1,
        &quot;sum_other_doc_count&quot; : 498,
        &quot;buckets&quot; : [
          {
            &quot;key&quot; : 934.9500122070312,
            &quot;doc_count&quot; : 8,
            &quot;top_revenue_hits&quot; : {
            &quot;value&quot; : 7479.60009765625
            }
          },
          {
            &quot;key&quot; : 922.0,
            &quot;doc_count&quot; : 6,
            &quot;top_revenue_hits&quot; : {
            &quot;value&quot; : 5532.0
            }
          },
          ...
        ]
      }
    }
  }
  </code></pre>
  <p>How can I get the query to return the entire <code>guests</code> object, not only the sum amount.</p>
  <p>When I run <code>GET reservations/_search</code> it returns:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;hits&quot;: [
      {
        &quot;_index&quot;: &quot;reservations&quot;,
        &quot;_id&quot;: &quot;1334620&quot;,
        &quot;_score&quot;: 1.0,
        &quot;_source&quot;: {
          &quot;id&quot;: &quot;1334620&quot;,
          &quot;total&quot;: 110.8,
          &quot;payment&quot;: &quot;unpaid&quot;,
          &quot;contact&quot;: {
            &quot;name&quot;: &quot;John Doe&quot;,
            &quot;email&quot;: &quot;john@mail.com&quot;
          }
        }
      },
      ... other reservations
    ]
  }
  </code></pre>
  <p>I want to get this to return with the <code>sum</code> aggregation.</p>
  <p>I have tried to use a <code>top_hits</code> aggregation, using <code>_source</code> it does return the entire <code>guest</code> object but it does not show the total amount spent. And when adding <code>_source</code> to the <code>sum</code> aggregation it gives an error.</p>
  <p>Can I return the entire <code>guest</code> object with a <code>sum</code> aggregation or is this not the correct way?</p>