# MUST\_NOT not working with EXIST in NESTED query

**URL:** <https://discuss.elastic.co/t/must-not-not-working-with-exist-in-nested-query/101952>\
**Category:** Elasticsearch\
**Created:** [September 27, 2017, 8:56am UTC](https://discuss.elastic.co/t/must-not-not-working-with-exist-in-nested-query/101952 "2017-09-27T08:56:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlp1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jlp1/32/6607_2.png) [@jlp1](https://discuss.elastic.co/u/jlp1)\
**Post date:** [September 27, 2017, 8:56am UTC](https://discuss.elastic.co/t/must-not-not-working-with-exist-in-nested-query/101952/1 "2017-09-27T08:56:21Z")

</div>

ES 5.2.2  
Here is my mapping:

```
PUT players
{
   "mappings": {
      "players": {
         "properties": {
            "features": {
               "type": "nested",
               "properties": {
                  "name": {
                     "type": "text",
                     "fields": {
                        "keyword": {
                           "type": "keyword",
                           "ignore_above": 256
                        }
                     }
                  },
                  "value": {
                     "type": "long"
                  }
               }
            },
            "name": {
               "type": "text",
               "fields": {
                  "keyword": {
                     "type": "keyword",
                     "ignore_above": 256
                  }
               }
            }
         }
      }
   }
}

```

And some docs:

```
PUT players/players/1
{
    "name": "test"
}

PUT players/players/2
{
    "name": "Neuer",
    "features": [{
        "name": "handling",
        "value": 94
    }]
}

PUT players/players/3
{
    "name": "Lewandowski",
    "features": [{
        "name": "positioning",
        "value": 85
    }]
}

```

Now, I want to get players that do not have features at all.

I started with simple query to get players with no features - but it returns all players.

```
POST players/players/_search
{
   "query": {
      "bool": {
         "filter": [
            {
               "bool": {
                  "must_not": [
                     {
                        "exists": {
                           "field": "features"
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

```

So I tried a nested query, but it returns no result.

```
POST players/players/_search
{
   "query": {
      "bool": {
         "filter": [
            {
               "nested": {
                  "path": "features",
                  "query": {
                     "bool": {
                        "must_not": [
                           {
                              "exists": {
                                 "field": "features"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

```

but the reversed query returns 2 elements so it is OK.

```
POST players/players/_search
{
   "query": {
      "bool": {
         "filter": [
            {
               "nested": {
                  "path": "features",
                  "query": {
                     "bool": {
                        "must": [
                           {
                              "exists": {
                                 "field": "features"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

```

How the query should look like to get players with no features?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 27, 2017, 6:29pm UTC](https://discuss.elastic.co/t/must-not-not-working-with-exist-in-nested-query/101952/2 "2017-09-27T18:29:35Z")

</div>

If instead of putting the `must_not` inside your `nested` query, you put the `nested` query inside of a `must_not`, it should work:

```
POST players/players/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "features",
            "query": {
              "exists": {
                "field": "features"
              }
            }
          }
        }
      ]
    }
  }
}
```

---

<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:** [October 25, 2017, 6:30pm UTC](https://discuss.elastic.co/t/must-not-not-working-with-exist-in-nested-query/101952/3 "2017-10-25T18:30:19Z")

</div>

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