# Nested query builder inside must\_not clause does not build inner hits

**URL:** <https://discuss.elastic.co/t/nested-query-builder-inside-must-not-clause-does-not-build-inner-hits/286210>\
**Category:** Elasticsearch\
**Created:** [October 8, 2021, 9:07am UTC](https://discuss.elastic.co/t/nested-query-builder-inside-must-not-clause-does-not-build-inner-hits/286210 "2021-10-08T09:07:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cip999](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cip999/32/95593_2.png) [@cip999](https://discuss.elastic.co/u/cip999)\
**Post date:** [October 8, 2021, 9:07am UTC](https://discuss.elastic.co/t/nested-query-builder-inside-must-not-clause-does-not-build-inner-hits/286210/1 "2021-10-08T09:07:25Z")

</div>

I encountered an unexpected behavior of ES when combining `must_not` and `nested`.

Let's say I have an index `my_index` with the following schema:

```auto
{
  "mappings": {
    "properties": {
      "normal_field": {
        "type": "long"
      },
      "nested_field": {
        "type": "nested",
        "fields": {
          "subfield": {
            "type": "long"
          }
        }
      }
    }
  }
}

```

I would like to get all the inner hits for the field `nested_field.subfield`, subject to the condition `not (normal_field = 6 or nested_field.subfield > 1990)`.

The logical way to perform this query would be this:

```auto
"bool": {
  "must_not": [{
    "bool": {
      "must": [{
        "term": {
          "normal_field": {
            "value": 6
          }
        }
      }, 
      {
        "nested": {
          "query": {
            "range": {
              "nested_field.subfield": {
                "from": 1990,
                "to": null,
                "include_lower": false
              }
            }
          },
          "path": "nested_field",
          "inner_hits": {
            "from": 0,
            "size": 100,
            "_source": {
              "includes": ["nested_field.subfield"],
              "excludes": []
            }
          }
        }
      }]
    }
  }]
}

```

However, for some reason ES returns the correct matching documents, yet with no inner hits.

Now, in this specific case the condition can be re-written equivalently as `(not normal_field = 6) or (not nested_field.subfield > 1990)`. That is, one can query the index through a bool query builder with two `must_not` clauses, of which one is a term query and one is a nested query (which, in turn, contains a range query). It turns out that this way ES does build inner hits as expected (more generally, my understanding is that this works as long as the `must_not` clauses do not contain any bool query builders).

The problem is that there can be much more complicated queries which exhibit the same strange behavior, and it is not always clear how to rewrite them so as to bring the `must_not` clauses all the way up to the innermost level.

Has anyone else experienced the same issue? Are there any workarounds?

Thanks!

---

<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:** [November 5, 2021, 9:07am UTC](https://discuss.elastic.co/t/nested-query-builder-inside-must-not-clause-does-not-build-inner-hits/286210/2 "2021-11-05T09:07:58Z")

</div>

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