title: Filtering collapsed results in Elasticsearch
body: <p>I have an elasticsearch index containing documents that represent entities at a given point in time. When an entity changes state, a new document is created with a timestamp. When I need to get the current state of all entities, I can do the following:</p>
  
  <pre><code>GET https://127.0.0.1:9200/myindex/_search
  {
      "collapse": {
          "field": "entity_id"
      },
      "sort" : [{
          "timestamp": {
              "order": "desc"
          }
      }]
  }
  </code></pre>
  
  <p>However, I would like to further filter the result of the collapse. When entities are deleted I create a new document that includes an is_deleted flag along with the timestamp in a nested metadata field. I would like to extend the above query to entirely filter out those entities that have been deleted. Using a term filter on <code>entity_metadata.is_deleted: true</code> obviously does not work, because then my result just includes the last document with that entity_id before it got marked as deleted. How can I filter my results <em>after</em> the collapse is done to exclude any tombstoned entites?</p>