# Execute not-existing filter on nested document that may not be present in mapping

**URL:** <https://discuss.elastic.co/t/execute-not-existing-filter-on-nested-document-that-may-not-be-present-in-mapping/190004>\
**Category:** Elasticsearch\
**Created:** [July 11, 2019, 12:14pm UTC](https://discuss.elastic.co/t/execute-not-existing-filter-on-nested-document-that-may-not-be-present-in-mapping/190004 "2019-07-11T12:14:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emil/32/49885_2.png) [@Emil](https://discuss.elastic.co/u/Emil)\
**Post date:** [July 11, 2019, 12:14pm UTC](https://discuss.elastic.co/t/execute-not-existing-filter-on-nested-document-that-may-not-be-present-in-mapping/190004/1 "2019-07-11T12:14:24Z")

</div>

I have a question that is related to [this one](https://discuss.elastic.co/t/execute-a-filter-on-nested-document-only-if-it-exists/72720) (execute a filter on nested document only if it exists), but with a twist.

The older question is about filtering on a nested document, but this approach fails if a nested field is present in the mapping of one index, but not the other.

To make it clearer, here is an example:

```auto
PUT temp1?include_type_name=false
{
  "mappings": {
    "properties": {
      "nst": {
        "type": "nested",
        "properties": {
          "a": {
            "type": "keyword"
          },
          "b": {
            "type": "keyword"
          }
        }
      },
      "foo": {
        "type": "text"
      },
      "bar": {
        "type": "text"
      }
    }
  }
}

PUT temp2?include_type_name=false
{
  "mappings": {
    "properties": {
      "foo": {
        "type": "text"
      }
    }
  }
}

PUT temp1/_doc/1
{
  "nst": {
    "a": 1,
    "b": 2
  },
  "foo": "my text",
  "bar": "more text"
}
PUT temp2/_doc/1
{
  "foo": "some other text"
}

```

Now if I want to extract all documents from both indices, that don't have a field `bar`, that's easy:

```auto
GET temp*/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "bar"
          }
        }
      ]
    }
  }
}

```

But what if I want to get all documents that don't have a field `nst`?

Simply replacing bar with `nst` or `nst.a` doesn't work, as `nst` is not a field of itself , and `nst.a` is a nested field, so it's not accessible. This means `exists` returns `false` for both, and I get 2 documents.  
But I get an error if I follow the approach for a nested field:

```auto
GET temp*/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "nst",
            "query": {
              "exists": {
                "field": "nst.a"
              }
            }
          }
        }
      ]
    }
  }
}

```

For the index `temp2`, the query `failed to find nested object under path [nst]`.  
Understandable, but is there a way to still query for this?  
The only way I can think of is including a nested field `nst` in each of my indices, with the sole purpose of leaving it empty. That seems ugly, and in my real data I have more then 2 indices.  
Any ideas?

---

<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:** [August 8, 2019, 12:14pm UTC](https://discuss.elastic.co/t/execute-not-existing-filter-on-nested-document-that-may-not-be-present-in-mapping/190004/2 "2019-08-08T12:14:27Z")

</div>

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