es scroll 时间_ES scroll(ES游标) 解决深分页

Elasticsearch Scroll提供了一种游标方式解决深分页问题,通过固定时间窗口保持搜索上下文,避免全局排序的代价。滚动搜索不适用于实时请求,适合大数据处理。注意设置合适的scroll时间,避免过多占用资源。Scanning scroll无排序且不支持聚合,适合快速遍历所有文档。长时间未使用的scroll_id可能导致SearchContextMissingException异常。
摘要由CSDN通过智能技术生成

ES scroll(ES游标) 解决深分页。

Why

当Elasticsearch响应请求时,它必须确定docs的顺序,排列响应结果。如果请求的页数较少(假设每页20个docs), Elasticsearch不会有什么问题,但是如果页数较大时,比如请求第20页,Elasticsearch不得不取出第1页到第20页的所有docs,再去除第1页到第19页的docs,得到第20页的docs。

原理

Scrolling allows us to do an initial search and to keep pulling batches of results from Elasticsearch until there are no more results left. It’s a bit like a cursor in a traditional database.

A scrolled search takes a snapshot in time(适时). 中间更新不可见。

深分页的代价是全局排序,若禁止排序,sort by _doc,return the next batch of results from every shard that still has results to return.

context keepalive time(当批够用) 和 scroll_id(最新)

Set the scroll value to the length of time we want to keep the scroll window open.

How long it should keep the “search context” alive.

The scroll expiry time is refreshed every time we run a scroll request,所以不宜过长(垃圾)、过短(超时),够处理一批数据即可。

scroll parameter : how long it should keep the search context alive,long enough to process the previous batch of results, each scroll request sets a new expiry time.

An open search context prevents the old segments from being deleted while they are still in use.

注意:Keeping older segments alive means that more file handles(FD) are needed.

检查有多少search contexts(open_contexts):

Clear scroll API

Search context are automatically removed when the scroll timeout has been exceeded.

size

When scanning, the size is applied to each shard, 真实size是:size * number_of_primary_shards.

否则(regular scroll),返还总的size。

查询结束

No more hits are returned. Each call to the scroll API returns the next batch of results until there are no more results left to return, ie the hits array is empty.

适用场景

Scrolling is not intended for real time(实时) user requests, but rather for processing large amounts of data.

scroll目的,不是处理实时的用户请求,而是为处理大数据的。

似快照

The results that are returned from a scroll request reflect the state of the index at the time that the initial search request was made, like a snapshot in time. Subsequent changes to documents (index, update or delete) will only affect later search requests.

聚合

If the request specifies aggs, only the initial search response will contain the aggs results.

顺序无关

不关心返回文档的顺序!

Scroll requests have optimizations that make them faster when the sort order is _doc. If you want to iterate over all documents regardless of the order, this is the most efficient option:

scanning and standard scroll

scanning scroll与standard scroll 查询几点不同:

1. scanning scroll 结果没有排序,结果顺序是doc入库时的顺序;

2. scanning scroll 不支持聚合

3. scanning scroll 最初查询结果的“hits”列表中不会包含结果

4. scanning scroll 最初查询中如果设定了“size”,是设定每个分片(shard)size的数量,若size=3,有5个shard,每次返回结果的最大值就是3*5=15。

示例

常见问题

scroll_id一样与否

异常:SearchContextMissingException

SearchContextMissingException[No search context found for id [721283]];

原因:scroll设置的时间过短了。

源码212">问源码(2.1.2)

scroll_id的生成:

…search.type.TransportSearchHelper#buildScrollId(…) 三个参数,搜索查询类型、结果信息、查询条件参数 TransportSearchQueryThenFetchAction.AsyncAction. finishHim()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch 使用游标(Cursor)机制是为了在大量数据分页查询时提供一种高效的方式来逐条获取结果,而不需要一次性加载所有结果到内存中,特别适合处理大数据集。当从 Elasticsearch 中检索大型文档集合时,你可以使用 `scroll` 或 `search_after` API。 1. **Scroll API**:这是传统的游标方式,在客户端维护一个持续查询的生命周期,通过设置一个时间窗口(比如 `scroll_size` 和 `scroll_timeout`),服务器返回部分结果,并在请求中附带一个 `scroll_id`,直到所有的结果都被遍历完或超过指定的滚动时间,这时需要发送一个新的 scroll 请求终止当前查询。 示例: ``` GET _search { "size": 10, "scroll": "5m", "query": {...} } GET _search/scroll?scroll=5m&scroll_id=<scroll_id> ``` 2. **Search After API**:这是一种更现代、资源友好的方式,它返回查询结果以及用于下一次搜索的“after”值,这样可以避免多次滚动。当你完成遍历时,不再需要发起新的请求,而是直接关闭搜索。 示例: ``` GET _search { "size": 0, "sort": [{"_id": {"order": "asc"}}], "aggs": {...}, "search_after": [...] } ``` 在查询响应中,找到 `_scroll_id` 并将下一次查询的 `search_after` 设置为上一次的 `next_after` 值。 使用游标时,你应该注意合理设置大小、滚动时间和频率,以避免不必要的网络通信和潜在的性能问题。同时,也要确保在不需要的时候及时停止滚动,释放资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值