# Почему не работает nested запрос с must\_not?

**URL:** <https://discuss.elastic.co/t/nested-must-not/281315>\
**Category:** Вопросы на русском языке\
**Created:** [August 13, 2021, 8:13am UTC](https://discuss.elastic.co/t/nested-must-not/281315 "2021-08-13T08:13:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvanTushin](https://avatars.discourse-cdn.com/v4/letter/i/dbc845/32.png) [@IvanTushin](https://discuss.elastic.co/u/IvanTushin)\
**Post date:** [August 13, 2021, 8:13am UTC](https://discuss.elastic.co/t/nested-must-not/281315/1 "2021-08-13T08:13:07Z")

</div>

Добрый день коллеги!

У меня есть следующий запрос:

```auto
{
    "query": {
        "bool": {
            "filter": [
                {
                    "nested": {
                        "path": "customer",
                        "query": {
                            "bool": {
                                "should": [
                                    {
                                        "term": {
                                            "customer.data": "школа"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "nested": {
                        "path": "customer",
                        "query": {
                            "bool": {
                                "must_not": {
                                    "terms": {
                                        "customer.id": [
                                            "7648158"
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

```

такой запрос не работает корректно (не происходит исключения по полю customer.id), однако, если переписать его следующим образом (вынести nested запрос в must\_not, то он отрабатывает):

```auto
"bool": {
            "filter": [
                {
                    "nested": {
                        "path": "customer",
                        "query": {
                            "bool": {
                                "should": [
                                    {
                                        "term": {
                                            "customer.data": "школа"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                  "bool": {
                    "must_not":{
                    "nested": {
                        "path": "customer",
                        "query": {
                                    "terms": {
                                        "customer.id": [
                                            "76481580"
                                        ]
                                    }
                                }
                    }
                }
                  }
                }
            ]
        }

```

почему так происходит? это ожидаемое поведение?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [August 13, 2021, 3:12pm UTC](https://discuss.elastic.co/t/nested-must-not/281315/2 "2021-08-13T15:12:35Z")

</div>

Я не знаю, что вы пытаетесь добиться и какая у вас структура документа, но эти запросы будут работать по разному, если у вас несколько вложенных документов по пути customer. Первый запрос будет **включать** документы где хотя-бы один nested customer **не имеет** `customer.id = 76481580`. Второй запрос будет **исключать** все документы в которых хотя-бы один документ **имеет** `customer.id = 76481580`. То есть в первом случае, вы фильтруете по всеобщности (ALL) и во втором случае - по существованию (ANY).

---

<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:** [September 10, 2021, 3:13pm UTC](https://discuss.elastic.co/t/nested-must-not/281315/3 "2021-09-10T15:13:16Z")

</div>

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