title: How to get unique documents in elastic search based on a field, and 'group by' the result based on other fields
body: <p>I have just begun with elastic search and need to solve a problem too complex for me. I have thousands of documents in the index from which I have to query a predefined number of documents (which can be a few thousand too), I have to find groups of documents based on some fields, out of the unique documents based on another field (number of unique documents can be up to a few hundred).</p>
  
  <p>A document in my Index looks like this:</p>
  
  <pre><code>{  
   "complexProperty1" : {
              "A" : "example",
              "B" : "1",
              "D" : true,
              "E" : "case",
              "F" : ["guide1","guide2"]
  },
     "complexProperty2" : {
              "X" : "10",
              "Y" : ["specimen1","specimen2"],
              "Z" : "blueprint"
  }
  }
  </code></pre>
  
  <p>Many documents will have <code>complexProperty1.A</code> as <strong>"example"</strong>. I want to include them once and the resulting documents need to be grouped by <code>complexProperty1.D</code> and <code>complexProperty1.E</code>, i.e. for each pair of <code>complexProperty1.D</code> and <code>complexProperty1.E</code>, I have a list of documents (I only need these documents in my result). I am using Nest to achieve this. </p>