首页 > 其他分享 >elasticsearch之exists查询

elasticsearch之exists查询

时间:2023-01-12 09:13:46浏览次数:47  
标签:name exists doc child 查询 elasticsearch address test

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

标签:name,exists,doc,child,查询,elasticsearch,address,test
From: https://www.cnblogs.com/wufengtinghai/p/17045448.html

相关文章

  • ElasticSearch的基本操作
    1、索引相关操作1.1、创建索引对比关系型数据库,创建索引就等同于创建数据库。向ES服务器发PUT请求:http://127.0.0.1:9200/shopping,shopping即为索引名。请求后,服......
  • 【postgresql】基于多个合并UNION (ALL)查询结果创建数据表CREATE TABLE table_name
    【postgresql】基于多个合并UNION(ALL)查询结果创建数据表CREATETABLEtable_nameCREATETABLEmovies.movies_aliyundriveASSELECT*FROMmovies."电视剧,纪录片"U......
  • Django条件查询When、Case
    目录Django条件查询When、Case1、model和数据准备2、When和Case操作新增字段返回3、条件搜索4、条件更新5、条件聚合Django条件查询When、Case这一篇笔记将介绍条件......
  • 什么是SEO?SEO重要吗?怎么查询seo?
    你知道吗?大约64%的有机流量来自搜索。只有大约10%来自社交媒体。想要成功地建立数字营销策略,你必须掌握搜索引擎。本SEO新手指南将向您介绍搜索引擎优化的概念。​什么是SEO......
  • thinkphp中或查询使用方法
    一:字符串条件查询 //直接实例化Model $user=M('user1'); var_dump($user->where('id=1ORage=55')->select())二:使用索引数组作为查询条件$u......
  • Oracle分页查询出现重复数据的解决方法
    在使用MybatisPlus分页功能时发现:前端查询第一页是没问题的,但是向后查询的时候数据始终是第一页的查询第一页的时候发现没有任何问题往后查询,比如查询第二页时数据......
  • MySql树形结构(多级菜单)查询设计方案
    背景又很久没更新了,很幸运地新冠引发了严重的上呼吸道感染,大家羊过后注意休息和防护工作中(尤其是传统项目中)经常遇到这种需要,就是树形结构的查询(多级查询),常见的场景有:......
  • oracle 多行合并成一行: listagg within group CONNECT BY 可以和递归方法一起使用查
    oracle多行合并成一行:listaggwithingroupCONNECTBY可以和递归方法一起使用查询路径:https://www.bbsmax.com/A/A7zgpjGYJ4/oracle多行合并成一行:listaggwit......
  • 5、媒体查询
    创作人员可以通过媒体查询定义浏览器在何种媒体环境占用使用指定的样式表。媒体查询可以用在下属几个地方使用:1、link元素的media属性2、style元素的media属性3、@impo......
  • Java课程设计之——Elasticsearch篇
    0、团队项目博客1、主要使用的技术及开发工具Elasticsearch7.17.3RESTAPIElasticsearchjavaAPIClient7.17.3Kibana7.17.3Jackson2.12.32、Elasticsearch......