# Must\_not with exists query on nested object returns excluded field

**URL:** <https://discuss.elastic.co/t/must-not-with-exists-query-on-nested-object-returns-excluded-field/316289>\
**Category:** Elasticsearch\
**Tags:** kql-kibana-query-language\
**Created:** [October 11, 2022, 8:40am UTC](https://discuss.elastic.co/t/must-not-with-exists-query-on-nested-object-returns-excluded-field/316289 "2022-10-11T08:40:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![myx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/myx/32/109182_2.png) [@myx](https://discuss.elastic.co/u/myx)\
**Post date:** [October 11, 2022, 8:40am UTC](https://discuss.elastic.co/t/must-not-with-exists-query-on-nested-object-returns-excluded-field/316289/1 "2022-10-11T08:40:11Z")

</div>

Hi all,  
I am querying using search API and tried a query using **must\_not exists** to get documents where there is no list of emails in a group within a class. However, it still returns documents with **email\_list** inside the **group** and **class**. The following is my query

```auto
{
	"query": {
		"bool": {
			"must_not": [
				{
					"exists": {
						"field": "class.group.email_list"
					}
				}
			]
		}
	}
}

```

My mapping related to the query is as follows

```auto
{
	"class": {
		"properties": {
			"id": {
				"type": "long"
			},
			...
			"status": {
				"type": "long"
			},
			"group": {
				"type": "nested",
				"properties": {
					"id": {
						"type": "long"
					},
					"email_list": {
						"type": "keyword"
					},
					...
				}
			}
		}
	}
}

```

I've also tried the following queries, which I found on StackOverflow to no avail

```auto
{
	"query": {
		"bool": {
			"must_not": [
				{
					"nested": {
						"path": "class.group",
						"query": {
							"exists": {
								"field": "email_list"
							}
						}
					}
				}
			]
		}
	}
}

```

I've also tried this query, however, it results in an error saying that the object "class" is not nested.

```auto
{
	"query": {
		"bool": {
			"must_not": [
				{
					"nested": {
						"path": "class",
						"query": {
							"exists": {
								"field": "group.email_list"
							}
						}
					}
				}
			]
		}
	}
}

```

How can I get documents where there is no **email\_list** in a **group** within the **class**? Any help would be great...

Thanks a lot

---

<div class="post-metadata">

**Author:** ![myx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/myx/32/109182_2.png) [@myx](https://discuss.elastic.co/u/myx)\
**Post date:** [October 12, 2022, 2:45am UTC](https://discuss.elastic.co/t/must-not-with-exists-query-on-nested-object-returns-excluded-field/316289/2 "2022-10-12T02:45:36Z")

</div>

I asked the same question in StackOverflow and got the answer as follows  
src: [kibana - Elasticsearch Must\_not with exists query on nested object returns excluded field - Stack Overflow](https://stackoverflow.com/a/74027379/12499690)

```auto
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "class.group",
            "query": {
              "exists": {
                "field": "class.group.email_list"
              }
            }
          }
        }
      ]
    }
  }
}

```

---

<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 9, 2022, 2:45am UTC](https://discuss.elastic.co/t/must-not-with-exists-query-on-nested-object-returns-excluded-field/316289/3 "2022-11-09T02:45:58Z")

</div>

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