ES之条件、分页及排序查询

ES之条件、分页及排序查询

一、条件查询
发送【GET】请求:http://127.0.0.1:9200/test-index-3/_doc/1001,参数如下

{
    "query":{
        "match":{
            "num":5
        }
    }
}

查询num为5的数据,结果如下

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

二、全量查询
1.发送【GET】请求:http://127.0.0.1:9200/test-index-3/_doc/1001,参数如下

{
    "query":{
        "match_all":{
        }
    }
}

这里参数为 match_all 结果如下

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 8,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "rvHxw30BJJ5e1YHw6meH",
                "_score": 1.0,
                "_source": {
                    "title": "test1",
                    "num": 1,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "r_Hxw30BJJ5e1YHw_2fX",
                "_score": 1.0,
                "_source": {
                    "title": "test2",
                    "num": 2,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "sPHyw30BJJ5e1YHwEGfB",
                "_score": 1.0,
                "_source": {
                    "title": "test3",
                    "num": 3,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "sfHyw30BJJ5e1YHwKWdp",
                "_score": 1.0,
                "_source": {
                    "title": "test4",
                    "num": 4,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "svHyw30BJJ5e1YHwN2dz",
                "_score": 1.0,
                "_source": {
                    "title": "test5",
                    "num": 5,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "s_Hyw30BJJ5e1YHwWWcU",
                "_score": 1.0,
                "_source": {
                    "title": "test6",
                    "num": 5,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "tPHyw30BJJ5e1YHwYmet",
                "_score": 1.0,
                "_source": {
                    "title": "test7",
                    "num": 5,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "tfHyw30BJJ5e1YHwbmfT",
                "_score": 1.0,
                "_source": {
                    "title": "test8",
                    "num": 5,
                    "date": "20211213"
                }
            }
        ]
    }
}

2.增加查询条件,发送【GET】请求:http://127.0.0.1:9200/test-index-3/_doc/1001,参数如下

{
    "query" : {
        "match_all" : {
        }
    },
    "from" : 4,
    "size" : 4,
    "_source" : [
        "title", "num"
    ],
    "sort" : {
        "num" : {
            "order" : "desc"
        }
    }
}

这里参数含义为
from :起始位置,初始值为0【实际使用时,(页码-1)*size】,例子中为第二页数据
size :每页数据条数
_source :过滤显示的数据
sort :排序,例子中根据num倒序
结果如下

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 8,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "sfHyw30BJJ5e1YHwKWdp",
                "_score": null,
                "_source": {
                    "num": 4,
                    "title": "test4"
                },
                "sort": [
                    4
                ]
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "sPHyw30BJJ5e1YHwEGfB",
                "_score": null,
                "_source": {
                    "num": 3,
                    "title": "test3"
                },
                "sort": [
                    3
                ]
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "r_Hxw30BJJ5e1YHw_2fX",
                "_score": null,
                "_source": {
                    "num": 2,
                    "title": "test2"
                },
                "sort": [
                    2
                ]
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "rvHxw30BJJ5e1YHw6meH",
                "_score": null,
                "_source": {
                    "num": 1,
                    "title": "test1"
                },
                "sort": [
                    1
                ]
            }
        ]
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值