elasticsearch之exists查询
一、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查询的更多相关文章
- 利用kibana插件对Elasticsearch进行bool查询
#bool查询#老版本的filtered查询已经被bool代替#用 bool包括 must should must_not filter来完成 ,格式如下:#bool:{# "filter ...
- elasticsearch GIS空间查询问题解决
在GIS行业的应用越来越广泛,GIS最常用根据区域进行空间数据查询 我定义了两个方法,一起来看一下: /** * geodistance filter * 一个过滤器来过滤基于一个特定的距离从 ...
- exists查询中子表可以是
exists查询中子表可以是’或则具体某一列 ,查询结果一致,因为exists只会返回 true或者false,一个boolean型的值
- Elasticsearch文档查询
简单数据集 到目前为止,已经了解了基本知识,现在我们尝试用更逼真的数据集,这儿已经准备好了一份虚构的JSON,关于客户银行账户信息的.每个文档的结构如下: { , , "firstname& ...
- Elasticsearch(GEO)空间检索查询
Elasticsearch(GEO)空间检索查询python版本 1.Elasticsearch ES的强大就不用多说了,当你安装上插件,搭建好集群,你就拥有了一个搜索系统. 当然,ES的集群优化和查 ...
- java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询
1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...
- java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)
1.条件match query查询 //条件查询match query @Test public void test10() throws UnknownHostException { //1.指定e ...
- java使用elasticsearch进行模糊查询-已在项目中实际应用
java使用elasticsearch进行模糊查询 使用环境上篇文章本人已书写过,需要maven坐标,ES连接工具类的请看上一篇文章,以下是内容是笔者在真实项目中运用总结而产生,并写的是主要方法和思路 ...
- elasticsearch 请求体查询方式整理
空查询(empty search) —{}— 在功能上等价于使用 match_all 查询, 正如其名字一样,匹配所有文档: GET /_search { "query": { & ...
- Elasticsearch 常用基本查询
安装启动很简单,参考官网步骤:https://www.elastic.co/downloads/elasticsearch 为了介绍Elasticsearch中的不同查询类型,我们将对带有下列字段的文 ...
随机推荐
- 现有一个接口DataOperation定义了排序方法sort(int[])和查找方法search(int[],int),已知类QuickSort的quickSort(int[])方法实现了快速排序算法
欢迎大家加入我的社区:http://t.csdn.cn/Q52km 社区中不定时发红包 文章目录 1.UML类图 2.源码: 3.优缺点分析 1.UML类图 2.源码: package com.bac ...
- 1、小程序Vant_WebApp组件库的安装步骤和简单使用
Vant 1.小程序对于npm的支持 目前,小程序当中已经支持使用npm安装的第三方包,通过使用这些第三方包,我们可以提高对小程序开发的效率,但是在小程序当中使用所谓的npm包有如下的三个限制 不能支 ...
- .Net Core - 使用事务IDbtransaction操作DBData
New一个流程 获取数据库连接字符串,实例化SqlConnection 打来数据库连接 Begin当前连接的事务(IDbTransaction) 操作数据库(操作数据库的时候一定要使用当前连接和事务修 ...
- Java多线程-ThreadPool线程池-2(四)
线程池是个神器,用得好会非常地方便.本来觉得线程池的构造器有些复杂,即使讲清楚了对今后的用处可能也不太大,因为有一些Java定义好的线程池可以直接使用.但是(凡事总有个但是),还是觉得讲一讲可能跟有助 ...
- C#和Open eVision Studio图像库联合编程-读取图像
OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image Files (*.t ...
- 【CVE-2022-0543】Redis Lua沙盒绕过命令执行复现
免责声明: 本文章仅供学习和研究使用,严禁使用该文章内容对互联网其他应用进行非法操作,若将其用于非法目的,所造成的后果由您自行承担,产生的一切风险与本文作者无关,如继续阅读该文章即表明您默认遵守该内容 ...
- 在 Spring 生态中玩转 RocketMQ
本文作者:饶子昊 - Spring Cloud Alibaba Committer,阿里云智能开发工程师. 01 Spring 生态介绍 根据 JVM EcoSystem Report 2021 最新 ...
- [Android开发学iOS系列] TableView展现一个list
TableView 基础 本文讲讲TableView的基本使用. 顺便介绍一下delegation. TableView用来做什么 TableView用来展示一个很长的list. 和Android中的 ...
- 写一个frida通杀脚本
1. 前言 过年对我来说和平常没什么区别,该干什么干什么. 之前没接触过 frida 这个工具,前几天用了一些时间学习了一下,相比于 xposed hook 框架,frida 相对于调试方面真的很方便 ...
- 树莓派蓝牙rfcomm协议通信
修改配置文件 手机使用 "蓝牙串口" 软件,树莓派上修改文件/etc/systemd/system/dbus-org.bluez.service ExecStart=/usr/li ...