title: Elastic Search Difficult Query with Grouping
body: <p>I am new to Elastic Search (1.7).
  I have the ES index of a client’s versions.</p>
  
  <p>Each client has a unique Id (client_id).</p>
  
  <p>There are many versions of a client can be in the index.</p>
  
  <p>Each version of a client has a unique id (version_id) for the client.</p>
  
  <p>Index Data Example:</p>
  
  <pre><code>{
    “client_id”: "1",
    "version_id": "1",
    “versionCreationDate”: "2015-11-06T10:02:22.597Z",
    “clientName”: “FirstName LastName”
  }
  …,
  {
    “client_id”: "1",
    "version_id": "n",
    “versionCreationDate”: …
    “clientName”: “FirstName LastName”
  },
  {
    “client_id”: "2",
    "version_id": "1",
    “versionCreationDate”: …
    “clientName”: “FirstName LastName”
  },
  …
  {
    “client_id”: "2",
    "version_id": "n",
    “versionCreationDate”: …
    “clientName”: “FirstName LastName”
  },
  …
  {
    “client_id”: "N",
    "version_id": "1",
    “versionCreationDate”: …
    “clientName”: “FirstName LastName”
  },
  …
  {
    “client_id”: "N",
    "version_id": "n",
    “versionCreationDate”: …
      “clientName”: “FirstName LastName”
  }
  </code></pre>
  
  <p>I need to find client’s versions by input parameters for query:</p>
  
  <p>&lt;<em>clientName</em>>, &lt;<em>VersionCreationDateMax</em>>.</p>
  
  <p>Client's versions should match &lt;<em>clientName</em>> (with fuzziness=auto). And any version must have maximum value of versionCreationDate for this client, but &lt;=  &lt;<em>VersionCreationDateMax</em>>. So the only one version of each client should be in search results (newest, but not more than &lt;<em>VersionCreationDateMax</em>>)</p>
  
  <p>Please help me in finding the filter/query to do that </p>