gpt4 book ai didi

python - Elasticsearch 分页的最佳方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 20:03:26 25 4
gpt4 key购买 nike

使用 Elasticsearch 进行分页的最佳方式是什么?目前,我正在开发一个在后端使用Python使用Elasticsearch的API,并且我的索引没有太多数据,所以默认情况下我们在前端使用JavaScript进行分页(到目前为止,我们还没有任何数据)问题)。

我想知道对于更大的索引,处理分页的最佳方法是什么:

最佳答案

Elasticsearch 中搜索结果分页的默认方式是使用 from/size参数。但是,这仅适用于前 10,000 个搜索结果。

如果您需要超越该范围,则可行的方法是 search_after .

如果您需要转储整个索引,并且它包含超过 10k 文档,请使用 scroll API .

有什么区别?

所有这些查询都允许检索部分搜索结果,但它们有重大差异。

from/size 是最便宜且最快的,如果使用 Elasticsearch,Google 将使用它来获取第二、第三等搜索结果页面。

滚动 API 很昂贵,因为它会在您创建第一个查询时创建一种索引快照,以确保在滚动结束时您将拥有开始时索引中存在的数据。执行滚动请求会消耗资源,并且并行运行许多请求会降低性能,因此请谨慎操作。

搜索介于两者之间:

search_after is not a solution to jump freely to a random page but rather to scroll many queries in parallel. It is very similar to the scroll API but unlike it, the search_after parameter is stateless, it is always resolved against the latest version of the searcher. For this reason the sort order may change during a walk depending on the updates and deletes of your index.

因此,它将允许您对 10k 以上进行分页,但可能会出现一些不一致的情况。

为什么有 10k 限制?

index.max_result_window 设置为 10k 作为硬限制以避免 out of memory situations :

index.max_result_window

The maximum value of from + size for searches to this index. Defaults to 10000. Search requests take heap memory and time proportional to from + size and this limits that memory.

切片滚动怎么样?

Sliced scroll只是进行普通滚动的一种更快的方法:它允许并行下载文档集合。切片只是滚动查询输出中文档的子集。

关于python - Elasticsearch 分页的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59105657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com