Filtering results by inner hits data

So we have run into a problem related to a bit more complex scenario, where we have to filter search results by values from inner hits. There was a possibility to filter those results after being returned from elastic, but this would impede functionality of our application (not even speaking of performance).
In our project we use Elasticsearch 5.2.2.

We have a simple index of entities with mapping:

PUT resource/_mapping/entity
{
  "properties": {
    "id": {
      "type": "keyword"
    },
    "name": {
      "type": "keyword"
    },
    "claims": {
      "type": "nested",
      "properties": {
        "claimid": {
          "type": "keyword"
        },
        "priority": {
          "type": "short"
        },
        "visibility": {
          "type": "keyword"
        }
      }
    }
  }
}

Here's a sample document in the index:

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"
    }
  ]
}

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.:

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"]}
      }
    }
  }
}

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"?

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.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.