null_value(空值)
null 值不能被索引或者搜索.当一个字段被设置成 null(或者一个空数组,或者值全为 null 的数组)时, 该字段将被视为没有值。
null_value 参数允许你用一个特殊的值替换一个显示的 null 值, 以确保这个字段能被索引和搜索。例如:
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"my_type": {
"properties": {
"status_code": {
"type": "keyword",
"null_value": "NULL" #1
}
}
}
}
}
'
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
"status_code": null
}
'
curl -XPUT 'localhost:9200/my_index/my_type/2?pretty' -H 'Content-Type: application/json' -d'
{
"status_code": [] #2
}
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"status_code": "NULL" #3
}
}
}
'
1
用NULL代替显示的null值。
2
没有显示的包含null的空数组,不会被null_value替换。
3
搜索NULL将返回文档1,而不会返回文档2。
重点
null_value必须设置成相同类型的参数。例如,一个long型的字段不能被设置成string类型的null_value。
解析:
null值比较特殊,传统的方式,无法搜索会报错
如下
PUT /my_index
{
"mappings": {
"my_type": {
"properties": {
"status_code": {
"type": "keyword"
}
}
}
}
}
PUT /my_index/my_type/1
{
"status_code": null
}
GET /my_index/_search
{
"query": {
"term": {
"status_code": null
}
}
}
查询报错
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "field name is null or empty"
}
],
"type": "illegal_argument_exception",
"reason": "field name is null or empty"
},
"status": 400
}
所以为了解决这个问题,采用一个值来替换null值的情况. 如开始时候的例子,采用"NULL" 字符串采替换null值的情况进行搜索.
Last updated
Was this helpful?