# Nested must\_not query does not return expected results

**URL:** <https://discuss.elastic.co/t/nested-must-not-query-does-not-return-expected-results/291944>\
**Category:** Elasticsearch\
**Created:** [December 15, 2021, 10:22am UTC](https://discuss.elastic.co/t/nested-must-not-query-does-not-return-expected-results/291944 "2021-12-15T10:22:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsjs2](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jsjs2](https://discuss.elastic.co/u/jsjs2)\
**Post date:** [December 15, 2021, 10:22am UTC](https://discuss.elastic.co/t/nested-must-not-query-does-not-return-expected-results/291944/1 "2021-12-15T10:22:59Z")

</div>

what is the expected behavior for a nested must\_not query?  
sample query:

```auto
{
    "query": {
        "bool": {
            "filter": [
                {
                    "nested": {
                        "path": "participants",
                        "inner_hits": {
                            "name": "participants.1",
                            "_source": [
                                "participants.domain"
                            ],
                            "size": 10
                        },
                        "query": {
                            "bool": {
                                "must_not": [
                                    {
                                        "term": {
                                            "participants.domain.keyword": {
                                                "value": "TO",
                                                "case_insensitive": true
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}

```

this query will return results with some "participants"."domain" = "TO", the inner\_hit indicates the result meet the search criteria because one of the "participants"."domain" is "FROM".

```auto
 "_source": {
                    "participants": [
                        {
                            "domain": "TO",
                        },
                        {
                            "domain": "FROM",
                        }
                    ]
                }...

```

But I would expect none of the "participants"."domain" to be "TO", so any document with "participants"."domain"="TO" should not be returned.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 15, 2021, 10:44pm UTC](https://discuss.elastic.co/t/nested-must-not-query-does-not-return-expected-results/291944/2 "2021-12-15T22:44:54Z")

</div>

A nested query matches if _any_ of the nested objects in a doc match the criteria.  
On that basis this is expected behaviour.  
You could use “copy\_to” in your mappings to copy information from child documents to a multi valued field in the root and then use a must\_not clause on that.

---

<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:** [January 12, 2022, 10:45pm UTC](https://discuss.elastic.co/t/nested-must-not-query-does-not-return-expected-results/291944/3 "2022-01-12T22:45:52Z")

</div>

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