0

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?

1 Answer 1

1

You can use must_not as an outer boolean query combined with your nested one.

{
 "query": {
   "bool": {
     "must_not": [
       {
         "nested": {
           "path": "users",
           "query": {
             "bool": {
               "filter": {
                 "exists": {
                   "field": "users"
                 }
               }
             }
           }
         }
       }
     ]
   }
 }

}

Verified on 5.3.0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.