ElasticSearch 7.x 三种分页搜索选型 from&size scroll search_after
目录三种分页方式from & sizescrollsearch_after分页方式比较from&size 与 search_afterscroll与 search_after经典报错参考:三种分页方式from & size类似关系型数据库中的limit & offset。如果查询语句未标明from & size,es查询默认只会查出10条数据。但是默认情况下
目录
三种分页方式
from & size
类似关系型数据库中的limit & offset。
如果查询语句未标明from & size,es查询默认只会查出10条数据。
但是默认情况下,不能使用 from & size 设置超过 10,000的数量。此限制是由 index.max_result_window 索引设置设置的保护措施。如果需要查询超过 10,000 的数据,一是将max_result_window 改大(不推荐),二是改用 search_after 参数
scroll
scroll 类似于sql中的cursor,使用scroll,每次只能获取一页的内容,然后会返回一个scroll_id。根据返回的这个scroll_id可以不断地获取下一页的内容
search_after
search_after 分页的方式是根据上一页的最后一条数据来确定下一页的位置,同时在分页请求的过程中,如果有索引数据的增删改查,这些变更也会实时的反映到游标上。但是需要注意,因为每一页的数据依赖于上一页最后一条数据,所以无法跳页请求。
分页方式比较
以下摘自ElasticSearch官方文档7.x版
from&size 与 search_after
避免使用 from 和 size 来分页太深或一次请求太多结果。搜索请求通常跨越多个分片。每个分片必须将其请求的命中和任何先前页面的命中加载到内存中。对于深页面或大型结果集,这些操作会显着增加内存和 CPU 使用率,从而导致性能下降或节点故障。默认情况下,您不能使用 from 和 size 来翻阅超过 10,000 次点击。此限制是由 index.max_result_window 索引设置设置的保护措施。如果您需要翻阅超过 10,000 次点击,请改用 search_after 参数。
scroll与 search_after
我们不再推荐使用滚动 API 进行深度分页。如果您需要在分页超过 10,000 个点击时保留索引状态,请使用带有时间点 (PIT) 的 search_after 参数。
经典报错
使用普通from & size超过默认10000时:
Result window is too large, from + size must be less than or equal to: [***] but was [***]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting.
参考:
官方文档
https://www.elastic.co/guide/en/elasticsearch/reference/7.x/paginate-search-results.html
Elasticsearch 三种分页方式
https://www.cnblogs.com/jpfss/p/10815206.html
ES分页看这篇就够了
https://blog.csdn.net/pony_maggie/article/details/105478557
更多推荐
所有评论(0)