Elasticsearch 5.x locate documents without nested field

0
down vote
favorite
I have an index with a nested object. Mapping is like -

{
  "mappings": {
    "my_type": {
      "properties": {
        "group": {"type": "string"},
        "users": {
          "type": "nested", 
           "properties": {
                 "first": {"type": "string"},
                 "last": {"type": "string"}
           }
        }
      }
    }
  }
}

I need to find all documents which have no 'users'. I can see how to find only document which have users -

GET /my_index/my_type/_search
{
  "query": {
      "nested": {
          "path": "users",
             "query": {
                 "bool": {
                    "must": [
                     {
                      "exists": {
                           "field": "users"
                          }
                      }
                  ]
                 }
          }
       }
    }
 }

but I don't know how to do the reverse. Any pointers?

Wrap your nested query in a must_not. The following should work:

GET /my_index/my_type/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "query": {
            "nested": {
              "path": "users",
              "query": {
                "exists": {
                  "field": "users"
                }
              }
            }
          }
        }
      ]
    }
  }
}

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