Return all the document missing some certain key (of type nested in the mapping)

I have a document of this structure
"_source":
{"id":1 ,
"myNestedField" :
[
{"nested_id":"u71","Name":"ABC"} ,
{"nested_id":"op90","Name":"EG"}
]
}
The type of the nested_id in the mapping is nested
what i want to retrieve is to get all the documents missing the myNestedField key
and example document that i want to retrieve is

"_source":
{"id":1}
Please help

You could try the below to get all doc with missing field or missing value.

GET /my_index/posts/_search
{
    "query" : {
        "constant_score" : {
            "filter": {
                "missing" : { "field" : "tags" }
            }
        }
    }
}

Ref: Dealing with Null Values | Elasticsearch: The Definitive Guide [2.x] | Elastic

1 Like

First Thank you for you help
but it returns this error

  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "unknown query [missing]",
        "line" : 5,
        "col" : 28
      }
    ],
    "type" : "parsing_exception",
    "reason" : "unknown query [missing]",
    "line" : 5,
    "col" : 28,
    "caused_by" : {
      "type" : "named_object_not_found_exception",
      "reason" : "[5:28] unknown field [missing]"
    }
  },
  "status" : 400
}

My dear this is the solution
GET my-index-000001/_search

{
  "query": {
    "bool": {
      "must_not": {
        "nested": {
          "path": "myNestedField",
          "query": {
            "exists": {
              "field": "myNestedField"
            }
          }
        }
      }
    }
  }
}