elasticsearch之exists查询
2023-03-15 22:46:55 时间
一、exists查询简介
elastic search提供了exists查询,用以返回字段存在值的记录,默认情况下只有字段的值为null或者[]的时候,elasticsearch才会认为字段不存在;
exists查询的形式如下,其中field用于指定要查询的字段名字;
{
"query": {
"exists": {
"field": "user"
}
}
}
二、测试数据准备
我们尽量模拟document中字段可能出现的各种形式,以便可以全面的测试以下exists查询;
PUT exists_test/_doc/1/
{
"name":"sam",
"age":30,
"man": true,
"child":["jhon", "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/2/
{
"name":"",
"age":30,
"man": true,
"child":["jhon", "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/3/
{
"name":null,
"age":30,
"man": true,
"child":["jhon", "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/4/
{
"age":30,
"man": true,
"child":["jhon", "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/5/
{
"name":"sam",
"age":null,
"man": true,
"child":["jhon", "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/6/
{
"name":"sam",
"man": true,
"child":["jhon", "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/7/
{
"name":"sam",
"age":30,
"man": null,
"child":["jhon", "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/8/
{
"name":"sam",
"age":30,
"child":["jhon", "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/9/
{
"name":"sam",
"age":30,
"man": true,
"child":["", "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/10/
{
"name":"sam",
"age":30,
"man": true,
"child":["", ""],
"address":{"country":"US"}
}
PUT exists_test/_doc/11/
{
"name":"sam",
"age":30,
"man": true,
"child":[null, "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/12/
{
"name":"sam",
"age":30,
"man": true,
"child":[null, null],
"address":{"country":"US"}
}
PUT exists_test/_doc/13/
{
"name":"sam",
"age":30,
"man": true,
"child":[],
"address":{"country":"US"}
}
PUT exists_test/_doc/14/
{
"name":"sam",
"age":30,
"man": true,
"child":null,
"address":{"country":"US"}
}
PUT exists_test/_doc/15/
{
"name":"sam",
"age":30,
"man": true,
"address":{"country":"US"}
}
PUT exists_test/_doc/16/
{
"name":"sam",
"age":30,
"man": true,
"child":["jhon", "lily"],
"address":{}
}
PUT exists_test/_doc/17/
{
"name":"sam",
"age":30,
"man": true,
"child":["jhon", "lily"],
"address":null
}
PUT exists_test/_doc/18/
{
"name":"sam",
"age":30,
"man": true,
"child":["jhon", "lily"]
}
PUT exists_test/_doc/19/
{
"name":"sam",
"age":0,
"man": true,
"child":["jhon", "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/20/
{
"name":"-",
"age":30,
"man": true,
"child":["jhon", "lily"],
"address":{"country":"US"}
}
PUT exists_test/_doc/21/
{
"name":";",
"age":30,
"man": true,
"child":["jhon", "lily"],
"address":{"country":"US"}
}
三、exists查询测试
对于字符串类型字段,当字段没有出现、字段值为null的情况下,则该字段不存在;字段值为空则计算为字段存在;
POST exists_test/_doc/_search/
{
"query":{
"bool":{
"must_not":{
"exists":{
"field":"name"
}
}
}
}
}
{
"took":3,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":2,
"max_score":1,
"hits":[
{
"_index":"exists_test",
"_type":"_doc",
"_id":"4",
"_score":1,
"_source":{
"age":30,
"man":true,
"child":[
"jhon",
"lily"
],
"address":{
"country":"US"
}
}
},
{
"_index":"exists_test",
"_type":"_doc",
"_id":"3",
"_score":1,
"_source":{
"name":null,
"age":30,
"man":true,
"child":[
"jhon",
"lily"
],
"address":{
"country":"US"
}
}
}
]
}
}
对于数字类型字段,当字段没有出现、字段值为null的情况下,则该字段不存在;
POST exists_test/_doc/_search/
{
"query":{
"bool":{
"must_not":{
"exists":{
"field":"age"
}
}
}
}
}
{
"took":1,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":2,
"max_score":1,
"hits":[
{
"_index":"exists_test",
"_type":"_doc",
"_id":"5",
"_score":1,
"_source":{
"name":"sam",
"age":null,
"man":true,
"child":[
"jhon",
"lily"
],
"address":{
"country":"US"
}
}
},
{
"_index":"exists_test",
"_type":"_doc",
"_id":"6",
"_score":1,
"_source":{
"name":"sam",
"man":true,
"child":[
"jhon",
"lily"
],
"address":{
"country":"US"
}
}
}
]
}
}
对于布尔类型字段,当字段没有出现、字段值为null的情况下,则该字段不存在;
POST exists_test/_doc/_search/
{
"query":{
"bool":{
"must_not":{
"exists":{
"field":"man"
}
}
}
}
}
{
"took":1,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":2,
"max_score":1,
"hits":[
{
"_index":"exists_test",
"_type":"_doc",
"_id":"8",
"_score":1,
"_source":{
"name":"sam",
"age":30,
"child":[
"jhon",
"lily"
],
"address":{
"country":"US"
}
}
},
{
"_index":"exists_test",
"_type":"_doc",
"_id":"7",
"_score":1,
"_source":{
"name":"sam",
"age":30,
"man":null,
"child":[
"jhon",
"lily"
],
"address":{
"country":"US"
}
}
}
]
}
}
对于数组类型字段,只要字段没有出现、字段值为null、字段值为空数组、字段值数组的所有元素都为null,则该字段不存在;
POST exists_test/_doc/_search/
{
"query":{
"bool":{
"must_not":{
"exists":{
"field":"child"
}
}
}
}
}
{
"took":1,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":4,
"max_score":1,
"hits":[
{
"_index":"exists_test",
"_type":"_doc",
"_id":"14",
"_score":1,
"_source":{
"name":"sam",
"age":30,
"man":true,
"child":null,
"address":{
"country":"US"
}
}
},
{
"_index":"exists_test",
"_type":"_doc",
"_id":"12",
"_score":1,
"_source":{
"name":"sam",
"age":30,
"man":true,
"child":[
null,
null
],
"address":{
"country":"US"
}
}
},
{
"_index":"exists_test",
"_type":"_doc",
"_id":"15",
"_score":1,
"_source":{
"name":"sam",
"age":30,
"man":true,
"address":{
"country":"US"
}
}
},
{
"_index":"exists_test",
"_type":"_doc",
"_id":"13",
"_score":1,
"_source":{
"name":"sam",
"age":30,
"man":true,
"child":[
],
"address":{
"country":"US"
}
}
}
]
}
}
对于对象字段,只要字段没有出现、字段值是空对象、字段值为null,则该字段不存在;
POST exists_test/_doc/_search/
{
"query":{
"bool":{
"must_not":{
"exists":{
"field":"address"
}
}
}
}
}
{
"took":40,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":3,
"max_score":1,
"hits":[
{
"_index":"exists_test",
"_type":"_doc",
"_id":"16",
"_score":1,
"_source":{
"name":"sam",
"age":30,
"man":true,
"child":[
"jhon",
"lily"
],
"address":{
}
}
},
{
"_index":"exists_test",
"_type":"_doc",
"_id":"18",
"_score":1,
"_source":{
"name":"sam",
"age":30,
"man":true,
"child":[
"jhon",
"lily"
]
}
},
{
"_index":"exists_test",
"_type":"_doc",
"_id":"17",
"_score":1,
"_source":{
"name":"sam",
"age":30,
"man":true,
"child":[
"jhon",
"lily"
],
"address":null
}
}
]
}
}
四、测试总结
elasticsearch对于各种类型字段判断字段是否存在的判断条件如下
1.对于字符串类型字段,当字段没有出现、字段值为null的情况下,则该字段不存在;字段值为空则计算为字段存在;
2.对于数字类型字段,当字段没有出现、字段值为null的情况下,则该字段不存在;
3.对于布尔类型字段,当字段没有出现、字段值为null的情况下,则该字段不存在;
4.对于数组类型字段,只要字段没有出现、字段值为null、字段值为空数组、字段值数组的所有元素都为null,则该字段不存在;
5.对于对象字段,只要字段没有出现、字段值是空对象、字段值为null,则该字段不存在;
相关文章
- 通过可视化数据分析提升测试质量
- 我从Icon编程语言中所学到的
- NLP入门之语音模型原理
- 关于数据存储未来的三项预测
- 声明式编程和命令式编程的比较
- 微软开始提供公共预览版Windows 8.1下载
- 宜信正式开源其AIOps落地三大利器
- 关于C语言结构体偏移的一点思考
- 态牛-Tech Neo 8月刊:语言的选择
- 京东自建数据中心核心技术解密——基础设施篇
- GCC 用 C++ 来编译
- 深入浅出:如何从0开始学习大数据挖掘分析?
- 未来这五年,软件工程师如何安身立命(下)
- 未来这五年,软件工程师如何安身立命(上)
- 如果你看不见你还能编程吗?
- 企业需要DCIM工具来做什么?
- 为什么软件开发方法论让你觉得糟糕?
- 揭秘10个大数据神话
- 解读:大数据分析及其数据来源
- 教你选对编程技能