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

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐