Nested query builder inside must_not clause does not build inner hits

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:

{
  "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:

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

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