title: ElasticSearch - Filter results by inner hits
body: <p>We have a simple index of entities with mapping:</p>
  
  <pre><code>PUT resource/_mapping/entity
  {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      },
      "claims": {
        "type": "nested",
        "properties": {
          "claimid": {
            "type": "keyword"
          },
          "priority": {
            "type": "short"
          },
          "visibility": {
            "type": "keyword"
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>Here's a sample document in the index:</p>
  
  <pre><code>POST resource/entity/
  {
    "id": "2",
    "name": "e2",
    "claims": [
      {
        "claimid": "c1",
        "priority": "2",
        "visibility": "M",
        "reqid" : "2"
      },
      {
        "claimid": "c2",
        "priority": "1",
        "visibility": "V",
        "reqid" : "2"
      },
      {
        "claimid": "c5",
        "priority": "3",
        "visibility": "H",
        "reqid" : "2"
      }
    ]
  }
  </code></pre>
  
  <p>And a query to filter documents by provided set of 'claims.claimid', then to sort by 'claims.priority', select the one with highest priority and return only the 'claims.visibility' e.g.:</p>
  
  <pre><code>GET resource/entity/_search/
  {
    "query": {
      "nested": {
        "path": "claims",
        "query": {
          "bool": {
            "must": [
              {
                "terms": {
                  "claims.claimid": [
                    "c1",
                    "c4",
                    "c5"
                  ]
                }
              }
            ]
          }
        },          
        "inner_hits": {
          "sort": [
            {
              "claims.priority": "asc"
            }
          ],
          "size":1,
          "_source":{"includes":["claims.visibility"]}
        }
      }
    }
  }
  </code></pre>
  
  <p>And finally the problem to be solved: how to modify the query to filter out documents having resulted in "H" for visibility with highest priority in inner hits? Or what other query will return a set of documents with visibility of highest priority filtered by provided claim ids, but only those where visibility is not "H"?</p>
  
  <p>A catch here is that we have to sort the documents having all types of visibilities and filter out those with resulting "H" on a complete list of results.</p>