1、背景

在咱们向es中写入数据时,有些时候数据写入到es中的是null,或者没有写入这个字段,那么这个时候在es中该如何查问出这种为null的数据呢?

2、需要

假如咱们的mapping存在 如下2个字段nameaddress,其中 namekeyword类型且应用了null_value来解决null值,address字段是text类型。

咱们插入数据时,存在nameaddress字段都不存在的,存在nameaddress[]null的数据,咱们须要查问进去这些数据。

3、筹备数据

3.1 创立mapping

PUT /index_null_value{  "mappings": {    "properties": {      "name":{        "type": "keyword",        "null_value": "--"      },      "address":{        "type": "text"      },      "age":{        "type": "integer",        "null_value": "-1"      }    }  }}

留神:

  1. null_value 须要和字段的 类型值 保持一致,比方上方的ageinteger类型,那么null_value的值就必须是integer类型
  2. null_value 不会批改 _source中的值
  3. []空数组是不会被null_value替换的,因为[]中不蕴含明确的null
  4. null_value能够润饰的数据类型无限,比方text类型不可应用。

3.2 插入数据

PUT /index_null_value/_bulk{"index":{"_id":0}}{"age":10}{"index":{"_id":1}}{"name":null,"address": null,"age":10}{"index":{"_id":2}}{"name":[],"address":[],"age":20}{"index":{"_id":3}}{"name":[null],"address":[null],"age":60}{"index":{"_id":4}}{"name":[null,"123"],"address":[null,"123"],"age":70}{"index":{"_id":5}}{"name":["123",null],"address":["123",null],"age":80}{"index":{"_id":6}}{"name":["123","456"],"address":["123","456"],"age":90}
  1. 数据中存在 nameaddress字段都不存在的
  2. 数据中存在 nameaddress字段 是 [] 的
  3. 数据中存在 nameaddress字段 是 [null] 的
  4. 数据中存在 nameaddress字段 都有值的

4、查问 name字段为null的数据

5、查问address不存在或值间接为null的数据


6、参考链接

1、https://www.elastic.co/guide/en/elasticsearch/reference/8.6/null-value.html
2、https://www.elastic.co/guide/en/elasticsearch/reference/8.6/query-dsl-exists-query.html