栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ES之多条件、范围查询

ES之多条件、范围查询

ES之多条件、范围查询

一、多条件查询
1.条件“且”,即查询"title"为"test6",且"num"为5的数据
【GET】请求:http://127.0.0.1:9200/test-index-1/_search,参数如下

{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "title": "test6"
                    }
                },
                {
                    "match":{
                        "num": 5
                    }
                }
            ]
        }
    }
}

结果如下

{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.7917595,
        "hits": [
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "s_Hyw30BJJ5e1YHwWWcU",
                "_score": 2.7917595,
                "_source": {
                    "title": "test6",
                    "num": 5,
                    "date": "20211213"
                }
            }
        ]
    }
}

2.条件“或”,即查询"title"为"test6",或"title"为"test8"的数据
【GET】请求:http://127.0.0.1:9200/test-index-1/_search,参数如下

{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "title": "test6"
                    }
                },
                {
                    "match":{
                        "title": "test8"
                    }
                }
            ]
        }
    }
}

结果如下

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.7917595,
        "hits": [
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "s_Hyw30BJJ5e1YHwWWcU",
                "_score": 1.7917595,
                "_source": {
                    "title": "test6",
                    "num": 5,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "tfHyw30BJJ5e1YHwbmfT",
                "_score": 1.7917595,
                "_source": {
                    "title": "test8",
                    "num": 5,
                    "date": "20211213"
                }
            }
        ]
    }
}

二、范围查询
查询“num”小于4的数据
gt:大于
gte:大于等于
lt:小于
lte:小于等于
【GET】请求:http://127.0.0.1:9200/test-index-1/_search,参数如下

{
    "query":{
        "bool":{
            "filter":{
                "range":{
                    "num":{
                        "lt":4
                    }
                }
            }
        }
    }
}

结果如下

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 0.0,
        "hits": [
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "rvHxw30BJJ5e1YHw6meH",
                "_score": 0.0,
                "_source": {
                    "title": "test1",
                    "num": 1,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "r_Hxw30BJJ5e1YHw_2fX",
                "_score": 0.0,
                "_source": {
                    "title": "test2",
                    "num": 2,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "sPHyw30BJJ5e1YHwEGfB",
                "_score": 0.0,
                "_source": {
                    "title": "test3",
                    "num": 3,
                    "date": "20211213"
                }
            }
        ]
    }
}

注意:should与must或filter在同一层级直接使用时,should会失效,需要加入参数"minimum_should_match":1,或者should当做子层级
共用时参考 https://blog.csdn.net/andy_5826_liu/article/details/103161654

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/676877.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号