ElasticSearch分页查询的实现
1、设置mapping
PUT /t_order
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings" : {
"properties" : {
"cancel_reason" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"cancel_time" : {
"type" : "date"
},
"create_time" : {
"type" : "date"
},
"create_user" : {
"type" : "long"
},
"delivery_type" : {
"type" : "byte"
},
"discount_amount" : {
"type" : "integer"
},
"expired_time" : {
"type" : "date"
},
"id" : {
"type" : "long"
},
"is_deleted" : {
"type" : "byte"
},
"is_pay" : {
"type" : "byte"
},
"is_postsale" : {
"type" : "byte"
},
"order_amount" : {
"type" : "integer"
},
"order_code" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"order_remark" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"order_status" : {
"type" : "byte"
},
"pay_amount" : {
"type" : "integer"
},
"pay_time" : {
"type" : "date"
},
"pay_type" : {
"type" : "byte"
},
"postage" : {
"type" : "integer"
},
"product_amount" : {
"type" : "integer"
},
"serial_code" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"shop_id" : {
"type" : "long"
},
"update_time" : {
"type" : "date"
},
"update_user" : {
"type" : "long"
},
"user_id" : {
"type" : "long"
}
}
} POST /t_order/_search
{
"query": {
"match_all": {}
}
}
2、添加测试数据
POST /t_order/_bulk
{
"id": 202208780570360889300,
"order_code": "20222379790329301675",
"order_amount": 1,
"pay_amount": 1,
"discount_amount": 0,
"product_amount": 1,
"order_status": 0,
"is_deleted": 0,
"user_id": 202208761977967681500,
"shop_id": 117979,
"expired_time": 1648558094000,
"postage": 0,
"cancel_time": 1648556595000,
"cancel_reason": "订单逾期未支付系统自动取消订单",
"order_remark": "",
"delivery_type": 2,
"pay_time": null,
"pay_type": 1,
"is_pay": 0,
"is_postsale": 0,
"create_time": 1648556294000,
"create_user": 1508761977967681500,
"update_time": 1648556595000,
"update_user": 999999
}
3、演示(show me the code):
### from + size [深度翻页不推荐使用 From + size]
#from + size 两个参数定义了结果页面显示数据的内容。
#from:未指定,默认值是 0,注意不是1,代表当前页返回数据的起始值。
#size:未指定,默认值是 10,代表当前页返回数据的条数。
POST /ds-trade_t_order/_search
{
"from": 0,
"size": 20,
"query": {
"match_all": {}
}
}
### searchAfter [官方文档强调:不再建议使用scroll API进行深度分页。如果要分页检索超过 Top 10,000+ 结果时,推荐使用:PIT + search_after。]
#part1:创建 PIT 视图,这是前置条件不能省。
POST /ds-trade_t_order/_pit?keep_alive=5m
#part2:创建基础查询语句,这里要设置翻页的条件。
POST /_search
{
"size": 20,
"track_total_hits": true,
"query": {
"match_all": {}
},
"pit": {
"id": "l9G1AwEQZHMtdHJhZGVfdF9vcmRlchY2M3VFTm9uZ1RrT1ltbWx5RDZvQllnABZaeDFQbHhSMVJGNktBZm5kakxqYTZBAAAAAAAAIhFCFml1Uy1Kb21pU2Zxdlc4OHhfWE1aSkEAARY2M3VFTm9uZ1RrT1ltbWx5RDZvQllnAAA="
},
"sort": [
{
"create_time": {
"order": "desc"
}
}
]
}
#part3:实现后续翻页:后续翻页都需要借助 search_after 指定前一页的最后一个文档的 sort 字段值。
POST /_search
{
"size": 20,
"track_total_hits": true,
"query": {
"match_all": {}
},
"pit": {
"id": "l9G1AwEQZHMtdHJhZGVfdF9vcmRlchY2M3VFTm9uZ1RrT1ltbWx5RDZvQllnABZaeDFQbHhSMVJGNktBZm5kakxqYTZBAAAAAAAAIhFCFml1Uy1Kb21pU2Zxdlc4OHhfWE1aSkEAARY2M3VFTm9uZ1RrT1ltbWx5RDZvQllnAAA="
},
"sort": [
{
"create_time": {
"order": "desc"
}
}
],
"search_after": [
1648557674000,
7
]
}
###scroll [全量或数据量很大时遍历结果数据,而非分页查询。]
#part1:指定检索语句同时设置 scroll 上下文保留时间
POST /t_order/_search?scroll=3m
{
"size": 20,
"query": {
"match_all": {}
}
, "sort": [
{
"create_time": {
"order": "desc"
}
}
]
}
#part2:指定检索语句同时设置 scroll 上下文保留时间
POST /_search/scroll
{
"scroll":"3m",
"scroll_id":"FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFml1Uy1Kb21pU2Zxdlc4OHhfWE1aSkEAAAAAACIXbhZaeDFQbHhSMVJGNktBZm5kakxqYTZB"
}
总结:
From+ size:需要随机跳转不同分页(类似主流搜索引擎)、Top 10000 条数据之内分页显示场景。
search_after:仅需要向后翻页的场景及超过Top 10000 数据需要分页场景。
Scroll:需要遍历全量数据场景 。而非翻页的场景(翻页场景scrol id 最多打开500个)。
max_result_window:调大治标不治本,不建议调过大。
PIT:本质是视图。
另外:根据实际经验得出一些参考意见:
1、search_after pit 不适用于商品列表分页查询(类似京猫这种商品列表),因为用户从商品列表进入商品详情,长时间停留在详情页查看后,返回商品列表继续翻页,此时
keep_alive 已经过期,出现无法翻页的错误。
2、Scroll 也会有上述问题。同时scroll也会有连接过多的问题,不适用于分页场景。
To prevent against issues caused by having too many scrolls open, the user is not allowed to open scrolls past a certain limit. By default,
the maximum number of open scrolls is 500. This limit can be updated with the search.max_open_scroll_context cluster setting.
参考资料————————————————
版权声明:本文为CSDN博主「铭毅天下」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/laoyang360/article/details/116472697
ElasticSearch分页查询的实现的更多相关文章
- elasticsearch 分页查询实现方案——Top K+归并排序
elasticsearch 分页查询实现方案 1. from+size 实现分页 from表示从第几行开始,size表示查询多少条文档.from默认为0,size默认为10,注意:size的大小不能超 ...
- Elasticsearch——分页查询From&Size VS scroll
Elasticsearch中数据都存储在分片中,当执行搜索时每个分片独立搜索后,数据再经过整合返回.那么,如果要实现分页查询该怎么办呢? 更多内容参考Elasticsearch资料汇总 按照一般的查询 ...
- ElasticSearch—分页查询
ElasticSearch查询—分页查询详解 Elasticsearch中数据都存储在分片中,当执行搜索时每个分片独立搜索后,数据再经过整合返回.那么,如何实现分页查询呢? 按照一般的查询流程来说,如 ...
- elasticsearch 分页查询实现方案
1. from+size 实现分页 from表示从第几行开始,size表示查询多少条文档.from默认为0,size默认为10, 注意:size的大小不能超过index.max_result_wind ...
- ElasticSearch——分页查询
前言 ElasticSearch实现分页查询,有3种方式,他们在数据查询中各自占据着不同的优势,因此在搜索引擎的数据分页过程中,如何更好地利用各自的优势来进行数据查询是一个非常重要的过程. 传统分页( ...
- Elasticsearch 分页查询
目录 前言 from + size search after scroll api 总结 参考资料 前言 我们在实际工作中,有很多分页的需求,商品分页.订单分页等,在MySQL中我们可以使用limit ...
- Elasticsearch分页查询
global index global CLIENT index = "guajibao-ipused-2019.10.13" CLIENT = Elasticsearch(hos ...
- Elasticsearch教程(九) elasticsearch 查询数据 | 分页查询
Elasticsearch 的查询很灵活,并且有Filter,有分组功能,还有ScriptFilter等等,所以很强大.下面上代码: 一个简单的查询,返回一个List<对象> .. ...
- elasticsearch查询之大数据集分页查询
一. 要解决的问题 search命中的记录特别多,使用from+size分页,直接触发了elasticsearch的max_result_window的最大值: { "error" ...
- Elasticsearch from/size-浅分页查询-深分页 scroll-深分页search_after深度查询区别使用及应用场景
Elasticsearch调研深度查询 1.from/size 浅分页查询 一般的分页需求我们可以使用from和size的方式实现,但是这种的分页方式在深分页的场景下应该是避免使用的.深分页的页次增加 ...
随机推荐
- 轻松玩转Makefile | 企业项目级Makefile实例
前言 本文展示了一个比较完整的企业项目级别的Makefile文件,包括了:文件调用,源文件.头文件.库文件指定,软件版本号.宏定义,编译时间,自动目录等内容. 1.目录架构 本文中所采用的目录架构,在 ...
- 未配置Datasource时, 启动 SpringBoot 程序报错的问题
SpringBoot will show error if there is no datasource configuration in application.yml/application.pr ...
- csplit命令
csplit命令 csplit命令将用PATTERN分隔的FILE文件输出到文件xx00.xx01....,并将每个文件的字节数输出到标准输出. 语法 csplit [OPTION]... FILE ...
- 微信小程序云开发项目-个人待办事项-03【主页】模块开发
上一篇: 微信小程序云开发项目-个人待办事项-02今日模块开发 https://blog.csdn.net/IndexMan/article/details/124497893 模块开发步骤 本篇介绍 ...
- Java集合框架学习(十五) ListIterator接口详解
ListIterator介绍 一个为list对象设计的迭代器,允许开发人员在2个方向上迭代,修改和获得list迭代位置. ListIterator 没有所谓当前元素. 它的游标位置总是位于previo ...
- cf的几道小题
1.E - Fridge 教训:做题的时候,没有想清楚问题,把问题复杂化了 #include <bits/stdc++.h> #define int long long using nam ...
- sentry-cli 的 windows 安装
项目搭建时,发现在使用高版本 sentry-cli 上传 pdb 文件后会报 404 错误,同事猜测高版本的 sentry-cli 会返回错误的地址,建议我用低版本的试一下 依据教程,我在 windo ...
- C++的strcat实现
#include <iostream> #pragma warning(disable:4996); using namespace std; char* t = (char*)mallo ...
- [BUUCTF][Web][GXYCTF2019]Ping Ping Ping 1
打开靶机对应URL 提示有ip参数 尝试构造url http://714ad4a2-64e2-452b-8ab9-a38df80dc584.node4.buuoj.cn:81/?ip=127.0.0. ...
- 项目实战:Qt监测操作系统物理网卡通断v1.1.0(支持windows、linux、国产麒麟系统)
需求 使用Qt软件开发一个检测网卡的功能. 兼容windows.linux,国产麒麟系统(同为linux) Demo windows上运行: 国产麒麟操作上运行: 功 ...