title: Filter results to remove documents with the same field value based on another field value (without aggregation)
body: <p>Given the following 4 objects in an elasticsearch index:</p>
  
  <pre class="lang-js prettyprint-override"><code>"hits": [
    {
      "_id": "0:0",
      "_source": {
        "id": 0,
        "version": 0,
        "published": true
      }
    },
    {
      "_id": "0:1",
      "_source": {
        "id": 0,
        "version": 1,
        "published": false,
        "latest": true
      }
    },
    {
      "_id": "1:0",
      "_source": {
        "id": 1,
        "version": 0,
        "published": true
      }
    },
    {
      "_id": "1:1",
      "_source": {
        "id": 1,
        "version": 1,
        "published": true,
        "latest": true
      }
    }
  ]
  </code></pre>
  
  <p>I would like to find the documents using these rules: </p>
  
  <ul>
  <li>with <code>published:true</code></li>
  <li>no duplicate <code>id</code> </li>
  <li>for documents with the same <code>id</code> the highest <code>version</code> should be returned. </li>
  </ul>
  
  <p>So for the above I'd like to get <code>0:0</code> and <code>1:1</code>:</p>
  
  <pre class="lang-js prettyprint-override"><code>"hits": [
    {
      "_id": "0:0",
      "_source": {
        "id": 0,
        "version": 0,
        "published": true
      }
    },
    {
      "_id": "1:1",
      "_source": {
        "id": 1,
        "version": 1,
        "published": true,
        "latest": true
      }
    }
  ]
  </code></pre>
  
  <p>I'm aware that I can use <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/top-hits.html" rel="nofollow">top_hits</a>, but I'd like to know if this is possible without it, such that the main <code>hits.hits</code> array will contain these results.</p>
  
  <p>I'd probably do the collapsing as follows: </p>
  
  <pre class="lang-js prettyprint-override"><code>{ 
    query  : {...},
    aggs : {
      ids: {
        terms: {
            field: "id"
        },
        aggs:{
            dedup:{
              top_hits:{ size:1, sort: {version : 'desc'} }
            }
          }    
      }
    }
  }
  </code></pre>
  
  <p>The reason I'm hoping to avoid using <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/top-hits.html" rel="nofollow">top_hits</a> is that I'll need to update the  result parser in our application. Also the <code>size</code> field will not work correctly if I do so.</p>