[ElasticSearch]查询嵌套字段值为null的文档

当索引结构大致如下:

GET index_name/_mapping
{
  "index_name" : {
    "mappings" : {
      "properties" : {
        "extra" : {
          "type" : "nested",
          "properties" : {
            "id" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "late_fee" : {
              "type" : "float"
            }
          }
        },
        "id" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    }
  }
}

里面存有数据,但是部分数据extra字段为空(null),需要找到这些数据进行重新上传(如下):

{
  "took" : 2731,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "index_name",
        "_id" : "xxxx",
        "_score" : 0.0,
        "_ignored" : [
        ],
        "_source" : {
          "extra" : null,
          "id" : "xxxxx"
        }
      }
    ]
  }
}

按照官方文档的个人理解,null则视为字段不存在,所以不能直接对null值查询,如果在创建索引的时候没有对字段增加null_value的值,比如:

PUT index-name
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "null_value": "NULL"
      }
    }
  }
}

该值相当于为空时的默认值,在推送这个字段值为空时会补充字符串NULL作为值,就可以进行字符串NULL查询了;但因为我是接手这个修改,就不对索引字段做处理了,而是直接从想办法查出值为null的数据了,最后根据文档和其他人的回答写出以下查询结构:

GET index_name/_search
{
  "_source": [
    "id", "extra"
  ], 
  "query" : {
    "bool" : {
      "must_not" : 
        {
          "nested": {
            "path": "extra",
            "query": {
              "bool": {
                "must": [
                  {
                    "exists": {
                      "field": "extra.xxxx" //extra某个字段
                    }
                  }
                ]
              }
            }
          }
        }  
      
    }
  }
}

即查出extra字段为空的数据。