整理下ES分页查询的四种解决方案。
查询语句:
curl-XGET"http://127.0.0.1:9200/my_index/_search"-H'Content-Type: application/json'-d'
{
"query": {
"match_all": {}
},
"from": 10,
"size": 5
}'
上面的查询表示从搜索结果中取第10条开始的5条数据。
这个查询语句在 Elasticsearch 集群内部是怎么执行?假设该索引只有primary shards,没有 replica shards,假设10个分片。搜索一般包括两个阶段,query 和 fetch 阶段,query 阶段确定要取哪些doc,fetch 阶段取出具体的 doc。
Client 发送一次搜索请求,node1 接收到请求,然后,node1 创建一个大小为 from + size 的优先级队列用来存结果,我们管 node1 叫 coordinating node。
coordinating node将请求广播到涉及到的 shards,每个 shard 在内部执行搜索请求,然后,将结果存到内部的大小同样为 from + size 的优先级队列里,可以把优先级队列理解为一个包含 top N 结果的列表。
每个 shard 把暂存在自身优先级队列里的数据返回给 coordinating node,coordinating node 拿到各个 shards 返回的结果后对结果进行一次合并,产生一个全局的优先级队列,存到自身的优先级队列里。
在上面的过程中,coordinating node 拿到 (from + size) * 分片数目 条数据,然后合并并排序后选择前面的 from + size 条数据存到优先级队列,以便 fetch 阶段使用。另外,各个分片返回给 coordinating node 的数据用于选出前 from + size 条数据,所以,只需要返回唯一标记 doc 的 _id 以及用于排序的 _score 即可,这样也可以保证返回的数据量足够小。
coordinating node 计算好自己的优先级队列后,query 阶段结束,进入 fetch 阶段。
query 阶段知道了要取哪些数据,但是并没有取具体的数据,这就是 fetch 阶段要做的。
coordinating node 发送 GET 请求到相关shards。
shard 根据 doc 的 _id 取到数据详情,然后返回给 coordinating node。
coordinating node 返回数据给 Client。
coordinating node 的优先级队列里有 from + size 个 _doc _id,但是,在 fetch 阶段,并不需要取回所有数据,在上面的例子中,前10条数据是不需要取的,只需要取优先级队列里的第11到15条数据即可。
需要取的数据可能在不同分片,也可能在同一分片,coordinating node 使用 multi-get 来避免多次去同一分片取数据,从而提高性能。
from+size查询方式在10000-50000条数据(1000到5000页)以内的时候还是可以的,但是如果数据过多的话,就会出现深分页问题。
举例说明:
Elasticsearch 的这种方式提供了分页的功能,同时,也有相应的限制。举个例子,一个索引,有10亿数据,分10个 shards,然后,一个搜索请求,from=1,000,000,size=100,这时候,会带来严重的性能问题,CPU,内存,IO,网络带宽。
为了解决上面的问题,elasticsearch提出了一个scroll滚动的方式。
scroll 类似于sql中的cursor,使用scroll,每次只能获取一页的内容,然后会返回一个scroll_id。根据返回的这个scroll_id可以不断地获取下一页的内容,所以scroll并不适用于有跳页的情景。
1、初始搜索请求应该在查询中指定 scroll 参数,如 ?scroll=1m,这可以告诉 Elasticsearch 需要保持搜索的上下文环境多久。
初始搜索:
GET /my_index/my_type/_search?scroll=1m
{
"query": {
"match_all": {}
},
"size": 1,
"from": 0
}
返回结果:
{
"_scroll_id": "DnF1ZXJ5VGhlbkZldGNoAwAAAAAAAJFwFkRJQkY5QWRRU2t1SkFDb2JPMlFEQXcAAAAAAACRcRZESUJGOUFkUVNrdUpBQ29iTzJRREF3AAAAAAAAkXIWRElCRjlBZFFTa3VKQUNvYk8yUURBdw==",
"took": 0,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 34,
"max_score": 1,
"hits": [
..................
]
}
}
返回结果包含一个 scroll_id,可以被传递给 scroll API 来检索下一个批次的结果。
2、每次对 scroll API 的调用返回了结果的下一个批次结果,直到 hits 数组为空。scroll_id 则可以在请求体中传递。scroll 参数告诉 Elasticsearch 保持搜索的上下文等待另一个3m。返回数据的size与初次请求一致。
二次搜索:
# post请求或者get请求都可以,注意请求路径中不要写索引名称了$curl-XPOST"http://127.0.0.1:9200/_search/scroll"-H'Content-Type: application/json'-d'
{
"scroll":"3m",
"scroll_id": "DnF1ZXJ5VGhlbkZldGNoAwAAAAAAAJOHFkRJQkY5QWRRU2t1SkFDb2JPMlFEQXcAAAAAAACTiBZESUJGOUFkUVNrdUpBQ29iTzJRREF3AAAAAAAAk4YWRElCRjlBZFFTa3VKQUNvYk8yUURBdw=="
}'
返回结果:
{"_scroll_id":"DnF1ZXJ5VGhlbkZldGNoAwAAAAAAAJOHFkRJQkY5QWRRU2t1SkFDb2JPMlFEQXcAAAAAAACTiBZESUJGOUFkUVNrdUpBQ29iTzJRREF3AAAAAAAAk4YWRElCRjlBZFFTa3VKQUNvYk8yUURBdw==","took":1,"timed_out":false,"terminated_early":true,"_shards":{"total":3,"successful":3,"skipped":0,"failed":0},"hits":{"total":34,"max_score":1,"hits":[.....................]}}
原理上来说可以把 scroll 分为初始化和遍历两步,初始化时将所有符合搜索条件的搜索结果缓存起来,可以想象成快照,在遍历时,从这个快照里取数据,也就是说,在初始化后对索引插入、删除、更新数据都不会影响遍历结果。因此,scroll 并不适合用来做实时搜索,而更适用于后台批处理任务,比如群发。
scroll 的方式,官方的建议不用于实时的请求(一般用于数据导出),因为每一个scroll_id 不仅会占用大量的资源,而且会生成历史快照,对于数据的变更不会反映到快照上。
search_after 分页的方式是根据上一页的最后一条数据来确定下一页的位置,同时在分页请求的过程中,如果有索引数据的增删改查,这些变更也会实时的反映到游标上。但是需要注意,因为每一页的数据依赖于上一页最后一条数据,所以无法跳页请求。
为了找到每一页最后一条数据,每个文档必须有一个全局唯一值,官方推荐使用 _uid 作为全局唯一值,其实使用业务层的 id 也可以。
参考:search_after
1、首次查询
# 必须使用文档中的唯一值的字段作为排序的规范,如果业务字段没有包含唯一字段,可以使用_id字段# 另外还需要传入个时间字段来同时进行排序$curl-XGET"http://127.0.0.1:9200/orchsym-kong-plugin-2022.06.14/_search"-H'Content-Type: application/json'-d'
{
"_source": [
"_id","request"
],
"query": {
"match": {
"type": "kong_access_log"
}
},
"size": 3,
"sort": [
{
"request.time": {
"order": "desc"
}
},
{
"_id": {
"order": "desc"
}
}
]
}'
查询返回结果
{"took":1,"timed_out":false,"_shards":{"total":3,"successful":3,"skipped":0,"failed":0},"hits":{"total":34,"max_score":null,"hits":[{"_index":"orchsym-kong-plugin-2022.06.14","_type":"doc","_id":"SrPbYYEB5aAT_arJwVfV","_score":null,"_source":{"request":{"orchsym_user_name":"1_1_1","remote_addr":"172.18.89.52","scheme":"http","method":"GET","upstream_uri":"/por-1/o3aek9yd2a/85e6db65-e5cd-3fb6-afb0-4b72fd5f615e/app/tctest","original_uri":"/por-1/propath/defaroute/app/tctest","uri":"/por-1/propath/defaroute/app/tctest","orchsym_app_id":"02195d19-a583-4f91-96b8-111315fea0a8","protocol":"HTTP/1.1","orchsym_request_id":"b9bf1a60-bb94-48d7-8eca-1ca922bdcb8e#3","orchsym_app_name":"test1","size":365,"orchsym_portal_id":1,"time":1655204191}},"sort":[1655204191000,"SrPbYYEB5aAT_arJwVfV"]},{"_index":"orchsym-kong-plugin-2022.06.14","_type":"doc","_id":"SbPbYYEB5aAT_arJwVfV","_score":null,"_source":{"request":{"orchsym_user_name":"1_1_1","remote_addr":"172.18.89.52","scheme":"http","method":"GET","upstream_uri":"/app/tctest","original_uri":"/por-1/propath/defaroute/app/tctest","uri":"/por-1/o3aek9yd2a/85e6db65-e5cd-3fb6-afb0-4b72fd5f615e/app/tctest","orchsym_app_id":"02195d19-a583-4f91-96b8-111315fea0a8","protocol":"HTTP/1.1","orchsym_request_id":"b9bf1a60-bb94-48d7-8eca-1ca922bdcb8e#3","orchsym_app_name":"test1","size":644,"orchsym_portal_id":1,"time":1655204191}},"sort":[1655204191000,"SbPbYYEB5aAT_arJwVfV"]},{"_index":"orchsym-kong-plugin-2022.06.14","_type":"doc","_id":"SLPbYYEB5aAT_arJwVfV","_score":null,"_source":{"request":{"remote_addr":"172.18.89.52","orchsym_user_name":"1_1_1","method":"GET","scheme":"http","upstream_uri":"/app/tctest","original_uri":"/por-1/propath/defaroute/app/tctest","uri":"/por-1/o3aek9yd2a/85e6db65-e5cd-3fb6-afb0-4b72fd5f615e/app/tctest","orchsym_app_id":"02195d19-a583-4f91-96b8-111315fea0a8","protocol":"HTTP/1.1","orchsym_request_id":"9187cfcc-6625-47a9-9c70-9f66cf9030ae#2","orchsym_app_name":"test1","size":644,"orchsym_portal_id":1,"time":1655204188}},"sort":[1655204188000,"SLPbYYEB5aAT_arJwVfV"]}]}}
(2)第二次查询
# 需要将前一次查询时,最后显示的sort字段值,作为参数传给下一次查询中的search_after字段# 当使用search_after参数时,from的值必须被设为0或者-1,也可以不写$curl-XGET"http://127.0.0.1:9200/orchsym-kong-plugin-2022.06.14/_search"-H'Content-Type: application/json'-d'
{
"_source": [
"_id","request"
],
"query": {
"match": {
"type": "kong_access_log"
}
},
"size": 4,
"search_after": [1655204188000,"SLPbYYEB5aAT_arJwVfV"],
"sort": [
{
"request.time": {
"order": "desc"
}
},
{
"_id": {
"order": "desc"
}
}
]
}'
第二次查询返回结果就会如下所示:
{"took":1,"timed_out":false,"_shards":{"total":3,"successful":3,"skipped":0,"failed":0},"hits":{"total":34,"max_score":null,"hits":[{"_index":"orchsym-kong-plugin-2022.06.14","_type":"doc","_id":"RrPbYYEB5aAT_arJwVfV","_score":null,"_source":{"request":{"remote_addr":"172.18.89.52","orchsym_user_name":"1_1_1","method":"GET","scheme":"http","upstream_uri":"/por-1/o3aek9yd2a/85e6db65-e5cd-3fb6-afb0-4b72fd5f615e/app/tctest","original_uri":"/por-1/propath/defaroute/app/tctest","uri":"/por-1/propath/defaroute/app/tctest","orchsym_app_id":"02195d19-a583-4f91-96b8-111315fea0a8","orchsym_request_id":"9187cfcc-6625-47a9-9c70-9f66cf9030ae#2","protocol":"HTTP/1.1","size":365,"orchsym_app_name":"test1","orchsym_portal_id":1,"time":1655204188}},"sort":[1655204188000,"RrPbYYEB5aAT_arJwVfV"]},{"_index":"orchsym-kong-plugin-2022.06.14","_type":"doc","_id":"RbPbYYEB5aAT_arJwVfV","_score":null,"_source":{"request":{"orchsym_user_name":"1_1_1","remote_addr":"172.18.89.52","scheme":"http","method":"GET","upstream_uri":"/app/tctest","original_uri":"/por-1/propath/defaroute/app/tctest","uri":"/por-1/o3aek9yd2a/85e6db65-e5cd-3fb6-afb0-4b72fd5f615e/app/tctest","orchsym_app_id":"02195d19-a583-4f91-96b8-111315fea0a8","protocol":"HTTP/1.1","orchsym_request_id":"8320d291-ac04-4a13-ba99-012f56780136#2","size":644,"orchsym_app_name":"test1","orchsym_portal_id":1,"time":1655204186}},"sort":[1655204186000,"RbPbYYEB5aAT_arJwVfV"]},{"_index":"orchsym-kong-plugin-2022.06.14","_type":"doc","_id":"R7PbYYEB5aAT_arJwVfV","_score":null,"_source":{"request":{"orchsym_user_name":"1_1_1","remote_addr":"172.18.89.52","method":"GET","scheme":"http","upstream_uri":"/por-1/o3aek9yd2a/85e6db65-e5cd-3fb6-afb0-4b72fd5f615e/app/tctest","original_uri":"/por-1/propath/defaroute/app/tctest","uri":"/por-1/propath/defaroute/app/tctest","orchsym_app_id":"02195d19-a583-4f91-96b8-111315fea0a8","protocol":"HTTP/1.1","orchsym_request_id":"8320d291-ac04-4a13-ba99-012f56780136#2","orchsym_app_name":"test1","size":365,"orchsym_portal_id":1,"time":1655204186}},"sort":[1655204186000,"R7PbYYEB5aAT_arJwVfV"]},{"_index":"orchsym-kong-plugin-2022.06.14","_type":"doc","_id":"GrPbYYEB5aAT_arJmlfF","_score":null,"_source":{"request":{"remote_addr":"172.18.89.52","orchsym_user_name":"1_1_1","scheme":"http","method":"GET","upstream_uri":"/por-1/o3aek9yd2a/85e6db65-e5cd-3fb6-afb0-4b72fd5f615e/app/tctest","original_uri":"/por-1/propath/defaroute/app/tctest","uri":"/por-1/propath/defaroute/app/tctest","orchsym_app_id":"02195d19-a583-4f91-96b8-111315fea0a8","orchsym_request_id":"b9bf1a60-bb94-48d7-8eca-1ca922bdcb8e#2","protocol":"HTTP/1.1","orchsym_app_name":"test1","size":365,"orchsym_portal_id":1,"time":1655204183}},"sort":[1655204183000,"GrPbYYEB5aAT_arJmlfF"]}]}}