一、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,则该字段不存在;

elasticsearch之exists查询的更多相关文章

  1. 利用kibana插件对Elasticsearch进行bool查询

    #bool查询#老版本的filtered查询已经被bool代替#用 bool包括 must should must_not filter来完成 ,格式如下:#bool:{#  "filter ...

  2. elasticsearch GIS空间查询问题解决

    在GIS行业的应用越来越广泛,GIS最常用根据区域进行空间数据查询     我定义了两个方法,一起来看一下: /** * geodistance filter * 一个过滤器来过滤基于一个特定的距离从 ...

  3. exists查询中子表可以是

    exists查询中子表可以是’或则具体某一列 ,查询结果一致,因为exists只会返回 true或者false,一个boolean型的值

  4. Elasticsearch文档查询

    简单数据集 到目前为止,已经了解了基本知识,现在我们尝试用更逼真的数据集,这儿已经准备好了一份虚构的JSON,关于客户银行账户信息的.每个文档的结构如下: { , , "firstname& ...

  5. Elasticsearch(GEO)空间检索查询

    Elasticsearch(GEO)空间检索查询python版本 1.Elasticsearch ES的强大就不用多说了,当你安装上插件,搭建好集群,你就拥有了一个搜索系统. 当然,ES的集群优化和查 ...

  6. java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询

    1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...

  7. java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)

    1.条件match query查询 //条件查询match query @Test public void test10() throws UnknownHostException { //1.指定e ...

  8. java使用elasticsearch进行模糊查询-已在项目中实际应用

    java使用elasticsearch进行模糊查询 使用环境上篇文章本人已书写过,需要maven坐标,ES连接工具类的请看上一篇文章,以下是内容是笔者在真实项目中运用总结而产生,并写的是主要方法和思路 ...

  9. elasticsearch 请求体查询方式整理

    空查询(empty search) —{}— 在功能上等价于使用 match_all 查询, 正如其名字一样,匹配所有文档: GET /_search { "query": { & ...

  10. Elasticsearch 常用基本查询

    安装启动很简单,参考官网步骤:https://www.elastic.co/downloads/elasticsearch 为了介绍Elasticsearch中的不同查询类型,我们将对带有下列字段的文 ...

随机推荐

  1. 4.MongoDB系列之索引(一)

    1. 执行计划查看 db.getCollection('users').find({'username': 'shenjian'}).explain('executionStats') 结果查看,先大 ...

  2. 动词时态=>3.现在时态和过去时态构成详解

    现在时态构成详解 一般现在时态 最容易构成的时态,直接加动词原形(字典当中显示的词条)就可以 第三人称"单数"的话需要加s 这是最容易出错的时态:容易将 现在的时间,和一般的状态: ...

  3. LcdTools如何导出内置画面为bmp图片

    运行LcdTools,先设置好图片所需分辨率参数,点击"画面设置"栏,修改下图所示参数 点击"画面设置"栏,在"画面资源"栏找到需要导出的画 ...

  4. 「MySQL高级篇」MySQL之MVCC实现原理&&事务隔离级别的实现

    大家好,我是melo,一名大三后台练习生,死去的MVCC突然开始拷打我! 引言 MVCC,非常顺口的一个词,翻译起来却不是特别顺口:多版本并发控制. 其中多版本是指什么呢?一条记录的多个版本. 并发控 ...

  5. onps栈移植说明(2)——编译器及os适配层移植

    2. 字节对齐及基础数据类型定义 协议栈源码(码云/github)port/include/port/datatype.h中根据目标系统架构(16位 or 32位)及所使用的编译器定义基础数据类型及字 ...

  6. Atcoder beginner contest 249 C-Just K(二进制枚举)

    题目大意:给你N个字符串,你可以从中选择任意数量的字符串,请统计在你的字串中,相同字母出现次数正好为K次的字母数.数据保证出现的字母都是小写字母. 1≤N≤15 1 ≤K≤N 一开始读题的时候读错了, ...

  7. 使用 html2canvas 将页面中某一部分转为图片下载

    今天在项目中遇到一个需求是将生成的二维码和一些背景作为海报,然后将海报以图片的形式下载 使用了 html2canvas  插件 import html2canvas from "html2c ...

  8. Burpsuite系列1--自动扫描

    第一章 简述     Burpsuite是基于Java的用于web安全的工具,能够进行爬虫.代理.编码.密码爆破等任务,并支持对XSS漏洞.文件包含等漏洞的主动扫描或被动扫描.burpsuite2.0 ...

  9. Oracle数据库的导出和导入

    本次数据库的导入导出操作是导出公司环境的Oracle数据库,再导入本地数据库,采用impdp和expdp命令进行导入导出操作. 一.导出52数据库 1.用system用户登录到数据库,查看是否有创建d ...

  10. js把秒数转换为HH:MM:SS及时分秒格式

    /** * 转为HH:MM:SS * @param second * @returns {string} * @private */ var _showTime = function (second) ...