How to match documents that don't have a specific nested object

Hi!

I'd like to find documents with nested objects that don't match a specific query.
For example, I want to match documents that don't have an Alice user.

If I follow the documentation, and execute the query below, I still have the document because John Smith doesn't contain Alice:

GET my-index-000001/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must_not": [
            { "match": { "user.first": "Alice" }}
          ]
        }
      }
    }
  }
}

Result (that should be empty):

"hits" : {
  "total" : {
    "value" : 1,
    "relation" : "eq"
  },
  "max_score" : 0.0,
  "hits" : [
    {
      "_index" : "my-index-000001",
      "_type" : "_doc",
      "_id" : "1",
      "_score" : 0.0,
      "_source" : {
        "group" : "fans",
        "user" : [
          {
            "first" : "John",
            "last" : "Smith"
          },
          {
            "first" : "Alice",
            "last" : "White"
          }
        ]
      }
    }
  ]
}

How can I manage that?

Thanks by advance!

I solved it with this query:

GET my-index-000001/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "user",
            "query": {
              "match": {
                "user.first": "Alice"
              }
            }
          }
        }
      ]
    }
  }
}

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