title: Dedup elasticsearch results using multiple fields as unique key
body: <p>There have been similar question asked to this (see <a href="https://stackoverflow.com/questions/25448186/remove-duplicate-documents-from-a-search-in-elasticsearch">Remove duplicate documents from a search in Elasticsearch</a>) but I haven't found a way to dedup using multiple fields as the "unique key". Here's a simple example to illustrate a bit of what I'm looking for:</p>
  
  <p>Say this is our raw data:</p>
  
  <pre><code>{ "name": "X", "event": "A", "time": 1 }
  { "name": "X", "event": "B", "time": 2 }
  { "name": "X", "event": "B", "time": 3 }
  { "name": "Y", "event": "A", "time": 4 }
  { "name": "Y", "event": "C", "time": 5 }
  </code></pre>
  
  <p>I would essentially like to get the distinct event counts based on name and event. I want to avoid double counting the event B which happened on the same name X twice, so the counts I'd be looking for are:</p>
  
  <pre><code>event: A, count: 2
  event: B, count: 1
  event: C, count: 1
  </code></pre>
  
  <p>Is there a way to set up an agg query as seen in the related question? Another option I've deliberated is to index the object with a special key field (i.e. "X_A", "X_B", etc.). I could then simply dedup on this field. I'm not sure which is a preferred approach, but I'd personally prefer not to index the data with extra metadata.</p>