当前位置:   article > 正文

elasticsearch之exists查询_es exists

es exists

一、exists查询简介

elastic search提供了exists查询,用以返回字段存在值的记录,默认情况下只有字段的值为null或者[]的时候,elasticsearch才会认为字段不存在;

exists查询的形式如下,其中field用于指定要查询的字段名字;

{
    "query": {
        "exists": {
            "field": "user"
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、测试数据准备

我们尽量模拟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"}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196

三、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"
                    }
                }
            }
        ]
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

对于数字类型字段,当字段没有出现、字段值为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"
                    }
                }
            }
        ]
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

对于布尔类型字段,当字段没有出现、字段值为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"
                    }
                }
            }
        ]
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

对于数组类型字段,只要字段没有出现、字段值为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"
                    }
                }
            }
        ]
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

对于对象字段,只要字段没有出现、字段值是空对象、字段值为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
                }
            }
        ]
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

四、测试总结

elasticsearch对于各种类型字段判断字段是否存在的判断条件如下
1.对于字符串类型字段,当字段没有出现、字段值为null的情况下,则该字段不存在;字段值为空则计算为字段存在;
2.对于数字类型字段,当字段没有出现、字段值为null的情况下,则该字段不存在;
3.对于布尔类型字段,当字段没有出现、字段值为null的情况下,则该字段不存在;
4.对于数组类型字段,只要字段没有出现、字段值为null、字段值为空数组、字段值数组的所有元素都为null,则该字段不存在;
5.对于对象字段,只要字段没有出现、字段值是空对象、字段值为null,则该字段不存在;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/807285?site
推荐阅读
相关标签
  

闽ICP备14008679号