# Nested path with inner\_hits and complex logical operations

**URL:** <https://discuss.elastic.co/t/nested-path-with-inner-hits-and-complex-logical-operations/229300>\
**Category:** Elasticsearch\
**Created:** [April 22, 2020, 3:20pm UTC](https://discuss.elastic.co/t/nested-path-with-inner-hits-and-complex-logical-operations/229300 "2020-04-22T15:20:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jo\_dev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jo_dev/32/45836_2.png) [@jo\_dev](https://discuss.elastic.co/u/jo_dev)\
**Post date:** [April 22, 2020, 3:20pm UTC](https://discuss.elastic.co/t/nested-path-with-inner-hits-and-complex-logical-operations/229300/1 "2020-04-22T15:20:50Z")

</div>

I have document with following nested mapping -\>  
A -\> B -\> C

While querying with following expression with inner\_hits to get matching part of the document -\>

> ((A.B.field1 == 1) AND (A.B.C.field2 == "Foo")) OR ((A.B.field1 == 3) AND (A.B.C.field2 == "Bar"))

```
"query": {
  "bool": {
    "filter": {
      "nested": {
        "inner_hits": {},
          "path": "A",
            "query": {
               "bool": {
                 "filter": {
                    "nested": {
                      "inner_hits": {},
                       "path": "B",
                       "query": {
                         "bool": {
                           "minimum_should_match": 1,
                           "should": [
                              {
                               "bool": {
                                 "must":[
                                   {
                                     {
                                       "bool": { "must": ["bool": {"filter": ["match": {A.B.field1: 1}]}]}
                                     },
                                    {
                                      "bool": { "filter": [{"nested": {"path": "A.B.C", inner_hits:{"name":"A.B.C.1"},"query": {"match":{A.B.C.field2: "Foo"}}}}]}
                                    }
                                  }
                                ]
                              }
                            },
                            {
                               "bool": {
                                 "must":[
                                   {
                                     {
                                       "bool": { "must": ["bool": {"filter": ["match": {A.B.field1: 3}]}]}
                                     },
                                    {
                                      "bool": { "filter": [{"nested": {"path": "A.B.C", inner_hits:{"name":"A.B.C.2"},"query": {"match":{A.B.C.field2: "Bar"}}}}]}
                                    }
                                  }
                                ]
                              }
                            },
                          ]
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

```

This returns response with inner\_hits matching conditions _A.B.C.field2 == "Foo"_ and _A.B.C.field2 == "Bar"_ **individually** , **i.e. without considering other conditions in the logical expression.**  
I need a programatic way to process the response to join the matching pieces (i.e. matching pieces for each of the nesting level). But, because inner\_hits "A.B.C.1" and "A.B.C.2" contains information which doesn't always fulfil the entire matching criteria, it is non-trivial to decide which inner\_hit to consider.  
Is there anyway to for all the inner\_hits to include part of the document which matches entire logical expression? (matching part of that nesting level, and entire sub-nesting below as is)  
This used to be the behaviour in some of the older versions without fix for [https://github.com/elastic/elasticsearch/pull/37645](https://github.com/elastic/elasticsearch/pull/37645)

This used to work say in 6.6.0, but not working in 6.7.1 onwards...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 20, 2020, 3:20pm UTC](https://discuss.elastic.co/t/nested-path-with-inner-hits-and-complex-logical-operations/229300/2 "2020-05-20T15:20:50Z")

</div>

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