title: Specify Elasticsearch aggregation fields when finding duplicates
body: <p>I am using the following ES query when looking for duplicates:</p>
  <pre><code>&quot;aggs&quot;: {
      &quot;duplicates&quot;: {
          &quot;terms&quot;: {
              &quot;field&quot;: &quot;phone&quot;,
              &quot;min_doc_count&quot;: 2,
              &quot;size&quot;: 99999,
              &quot;order&quot;: {
                  &quot;_term&quot;: &quot;asc&quot;
              }
          },
          &quot;aggs&quot;: {
              &quot;_docs&quot;: {
                  &quot;top_hits&quot;: {
                      &quot;size&quot;: 99999
                  }
              }
          }
      }
  }
  </code></pre>
  <p>It works well, it returns the key which in this case is the <code>phone</code>, and inside of it it returns all the matches. The main problem is exactly that, on the _source it brings everything, which is a lot of fields on my case, and I wanted to specify to bring only the ones I need. Example of what's returning:</p>
  <pre><code>        &quot;duplicates&quot;: {
                  &quot;1&quot;: {
                      &quot;key&quot;: &quot;1&quot;,
                      &quot;doc_count&quot;: 2,
                      &quot;_docs&quot;: {
                          &quot;hits&quot;: {
                              &quot;total&quot;: 2,
                              &quot;max_score&quot;: 1,
                              &quot;hits&quot;: [
                                  {
                                      &quot;_index&quot;: &quot;local:company_id:1:sync&quot;,
                                      &quot;_type&quot;: &quot;leads&quot;,
                                      &quot;_id&quot;: &quot;23&quot;,
                                      &quot;_score&quot;: 1,
                                      &quot;_source&quot;: {
                                          &quot;id&quot;: 23,
                                          &quot;phone&quot;: 123456,
                                          &quot;areacode_id&quot;: 426,
                                          &quot;areacode_state_id&quot;: 2,
                                          &quot;firstName&quot;: &quot;Brayan&quot;,
                                          &quot;lastName&quot;: &quot;Rastelli&quot;,
                                          &quot;state&quot;: &quot;&quot;, // .... and so on
  </code></pre>
  <p>I want to specify the fields that will be returned on the <code>_source</code>, is that possible?</p>
  <p>Another problem that I'm having is that I want to order the aggregation results by a specific field (by <code>id</code>) but if I put any field name instead of <code>_term</code> it gives me an error.</p>
  <p>Thank you!</p>