# Insert a must\_not match inside nested filtered query

**URL:** <https://discuss.elastic.co/t/insert-a-must-not-match-inside-nested-filtered-query/161519>\
**Category:** Elasticsearch\
**Created:** [December 19, 2018, 1:06pm UTC](https://discuss.elastic.co/t/insert-a-must-not-match-inside-nested-filtered-query/161519 "2018-12-19T13:06:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nedko\_Georgiev](https://avatars.discourse-cdn.com/v4/letter/n/b782af/32.png) [@Nedko\_Georgiev](https://discuss.elastic.co/u/Nedko_Georgiev)\
**Post date:** [December 19, 2018, 1:06pm UTC](https://discuss.elastic.co/t/insert-a-must-not-match-inside-nested-filtered-query/161519/1 "2018-12-19T13:06:14Z")

</div>

Here's my current query, which is returning 100 results, instead of 99.

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "filter": [
                  {
                    "match": {
                      "properties.key": "keyToInclude"
                    }
                  },
                  {
                    "match": {
                      "properties.value": "valueToInclude"
                    }
                  }
                ],
                "must_not": [
                  {
                    "match": {
                      "properties.key": "keyToExclude"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

```

There's one document which has the property key "keysToExclude", which is a nested object inside the properties array.  
Why is it not excluding that one document? Is my structure wrong or should I not use match inside must\_not or something else?

Thanks!

---

<div class="post-metadata">

**Author:** ![Nedko\_Georgiev](https://avatars.discourse-cdn.com/v4/letter/n/b782af/32.png) [@Nedko\_Georgiev](https://discuss.elastic.co/u/Nedko_Georgiev)\
**Post date:** [December 19, 2018, 1:12pm UTC](https://discuss.elastic.co/t/insert-a-must-not-match-inside-nested-filtered-query/161519/2 "2018-12-19T13:12:43Z")

</div>

And of course right after I've asked, I've found the answer or at least a possible solution:

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "filter": [
                  {
                    "match": {
                      "properties.key": "keyToInclude"
                    }
                  },
                  {
                    "match": {
                      "properties.value": "valueToInclude"
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "filter": [
                  {
                    "match": {
                      "properties.key": "keyToExclude"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

```

Please let me know if there's a better and easier way to do it.

---

<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 16, 2019, 1:23pm UTC](https://discuss.elastic.co/t/insert-a-must-not-match-inside-nested-filtered-query/161519/3 "2019-01-16T13:23:41Z")

</div>

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