How to return multiple inner hits in multiple nested sub-queries for the same path?

Viewed 1091

When I have multiple nested sub-queries for the same path, it seems the result will only include the inner hits result of the last nested sub-query. Is there a way to return all of the inner hits results for the multiple nested sub-queries?

e.g.

{
    "query": {
        "bool: {
            "must": [{
                "nested": {
                    "query": {...},
                    "path": "path_a",
                    "inner_hits": {}
                }
            },{
                "nested": {
                    "query": {...},
                    "path": "path_a",
                    "inner_hits": {}
                }
            }]
        }
    }
}
2 Answers

If you add a unique name to your inner_hits, then the result will basically contain a map of your inner hits as you're expecting.

Note: It seems that sometimes the inner hits contains extra query names (from the other nested queries) in the matched_queries, so it may need some post-processing

For the same path one needs to specify the nested path before its sub-queries.

Below is an example of searching, either via the match or the range, in the same nested path. You could modify the search according to your needs.

    GET index/_search
    {
      "query": {
        "nested": {
          "path": “path.subpath”,
          "query": {
            "bool": {
              "must": [
                { "match": { “path.subpath.match1”: “valueMatch” }},
                { "range" : {  “path.subpath.range1” :         {"gte": “rangeMatch”  }  }  
 }
              ]
            }
          }
        }
      }
    }

I hope this helps!

Related