Elasticsearch 空值處理實戰(zhàn)指南
1、引言
DELETE?my-index-000001
PUT?my-index-000001
{
??"mappings":?{
????"properties":?{
??????"status_code":?{
????????"type":?"keyword"
??????},
??????"title":?{
????????"type":?"text"
??????}
????}
??}
}
PUT?my-index-000001/_bulk
{"index":{"_id":1}}
{"status_code":null,"title":"just?test"}
{"index":{"_id":2}}
{"status_code":"","title":"just?test"}
{"index":{"_id":3}}
{"status_code":[],"title":"just?test"}
POST?my-index-000001/_search
POST?my-index-000001/_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
}
2、null_value 的含義
DELETE?my-index-000001
PUT?my-index-000001
{
??"mappings":?{
????"properties":?{
??????"status_code":?{
????????"type":???????"keyword",
????????"null_value":?"NULL"
??????}
????}
??}
}
PUT?my-index-000001/_bulk
{"index":{"_id":1}}
{"status_code":null}
{"index":{"_id":2}}
{"status_code":[]}
{"index":{"_id":3}}
{"status_code":"NULL"}
GET?my-index-000001/_search
{
??"query":?{
????"term":?{
??????"status_code":?"NULL"
????}
??}
}
相當于我們在 Mapping 定義階段指定了空的默認值,用“NULL”來代替,這樣做的好處:類似如上的_id = 1 的文檔,空字段也可以被索引、檢索。 不會再報 "field name is null or empty" 的錯誤了。
3、null_value 使用注意
null_value 必須和定義的數(shù)據(jù)類型匹配,舉例:long 類型字段不能有string 類型的 null value。
PUT?my-index-000001
{
??"mappings":?{
????"properties":?{
??????"status_code":?{
????????"type":?"keyword"
??????},
??????"title":?{
????????"type":?"long",
????????"null_value":?"NULL"
??????}
????}
??}
}
{
??"error":?{
????"root_cause":?[
??????{
????????"type":?"mapper_parsing_exception",
????????"reason":?"Failed?to?parse?mapping?[_doc]:?For?input?string:?\"NULL\""
??????}
????],
????"type":?"mapper_parsing_exception",
????"reason":?"Failed?to?parse?mapping?[_doc]:?For?input?string:?\"NULL\"",
????"caused_by":?{
??????"type":?"number_format_exception",
??????"reason":?"For?input?string:?\"NULL\""
????}
??},
??"status":?400
}
null_value 只影響了數(shù)據(jù)的索引,不會修改_source 文檔。
4、哪些字段有null_value, 哪些字段沒有null_value?
Arrays Boolean Date geo_point IP Keyword Numeric point
4.1 問題1:text 類型不支持 null_value 嗎?
DELETE?my-index-000001
PUT?my-index-000001
{
??"mappings":?{
????"properties":?{
??????"status_code":?{
????????"type":?"keyword"
??????},
??????"title":?{
????????"type":?"text",
????????"null_value":?"NULL"
??????}
????}
??}
}
{
??"error":?{
????"root_cause":?[
??????{
????????"type":?"mapper_parsing_exception",
????????"reason":?"Mapping?definition?for?[title]?has?unsupported?parameters:??[null_value?:?NULL]"
??????}
????],
????"type":?"mapper_parsing_exception",
????"reason":?"Failed?to?parse?mapping?[_doc]:?Mapping?definition?for?[title]?has?unsupported?parameters:??[null_value?:?NULL]",
????"caused_by":?{
??????"type":?"mapper_parsing_exception",
??????"reason":?"Mapping?definition?for?[title]?has?unsupported?parameters:??[null_value?:?NULL]"
????}
??},
??"status":?400
}
問題2:如果 text 類型也想設置空值,怎么搞呢?
PUT?my-index-000001
{
??"mappings":?{
????"properties":?{
??????"status_code":?{
????????"type":?"keyword"
??????},
??????"title":?{
????????"type":?"text",
????????"fields":?{
??????????"keyword":?{
????????????"type":?"keyword",
????????????"null_value":?"NULL"
??????????}
????????}
??????}
????}
??}
}
5、線上問題探討
老哥們,請教一個問題 ,我現(xiàn)在數(shù)據(jù)中有content這個字段,我想查詢這個字段不為空字符串,我用must_not不行。我貼下我的sql 死磕 Elasticsearch 技術交流群


POST?test_001/_search
{
??"query":?{
????"bool":?{
??????"filter":?{
????????"bool":?{
??????????"must":?[
????????????{
??????????????"exists":?{
????????????????"field":?"cont"
??????????????}
????????????},
????????????{
??????????????"term":?{
????????????????"content.keyword":?{
??????????????????"value":?""
????????????????}
??????????????}
????????????}
??????????]
????????}
??????}
????}
??}
}
POST?test_001/_search
{
??"query":?{
????"bool":?{
??????"filter":?{
????????"script":?{
??????????"script":?{
????????????"source":?"doc['content.keyword'].length?==?1",
????????????"lang":?"painless"
??????????}
????????}
??????}
????}
??}
}
6、小結
7、加餐-討論
評論
圖片
表情
