简体   繁体   English

如何用elasticsearch进行分页?来自vs scroll API

[英]how to do pagination with elasticsearch? from vs scroll API

I'm using elasticsearch as DB to store a large batch of log data. 我使用elasticsearch作为DB来存储大量的日志数据。 I know there are 2 ways to do pagination: 我知道有两种方法可以进行分页:

  1. Use size and from API 使用大小和API

  2. Use scroll API 使用滚动API

Now I'm using 'from' to do pagination.Get page and size parameters from front end,and at back end(Java) 现在我使用'from'来做分页。从前端和后端获取页面和大小参数(Java)

searchSourceBuilder.size(size);
searchSourceBuilder.from(page * size);

However, if page*size > 10000, an exception thrown from ES. 但是,如果page*size > 10000,则从ES抛出异常。

Can I use scroll API to do pagination? 我可以使用滚动API进行分页吗?

I know that if I use scroll API, the searchResponse object will return me a _scroll_id , which looks like a base64 string. 我知道如果我使用滚动API, searchResponse对象将返回一个_scroll_id ,它看起来像一个base64字符串。

How can I control page and size? 如何控制页面和大小?

It seems Scroll API only support successive page number? 看来Scroll API只支持连续的页码?

There is nothing in Elasticsearch which allows direct jump to a specific page as the results have to be collected from different shards. Elasticsearch中没有任何内容允许直接跳转到特定页面,因为必须从不同的分片收集结果。 So in your case search_after will be a better option. 所以在你的情况下, search_after将是一个更好的选择。 You can reduce the amount of data returned for the subsequent queries and then once you reach the page which is actually requested get the complete data. 您可以减少为后续查询返回的数据量,然后在到达实际请求的页面后获取完整数据。

Example: Let's say you have to jump to 99th page then you can reduce the amount of data for all 98th pages request and once you're at 99 you can get the complete data. 示例:假设您必须跳转到第99页,然后您可以减少所有第98页请求的数据量,一旦您达到99,您就可以获得完整的数据。

What you said is correct! 你说的是对的!

You can't do traditional pagination by using scroll API. 您无法使用滚动API进行传统分页。

I might suggest you to look the Search After API 我可能会建议您查看Search After API

This might not help you to cover your requirement! 这可能无法帮助您满足您的要求!

The From / Size default max result size is 10,000. From / Size默认最大结果大小为10,000。

As it is mentioned here 正如这里提到的那样

Note that from + size can not be more than the index.max_result_window index setting which defaults to 10,000 请注意, from + size不能超过index.max_result_window索引设置,默认为10,000

So if you somehow increase the index.max_result_window setting persistent then it will increase the max number of search result! 因此,如果您以某种方式增加index.max_result_window设置持久性,那么它将增加搜索结果的最大数量! But this might not be the solution but decrease the limitation. 但这可能不是解决方案,而是减少限制。

Remember this above solution might hamper the performance of your ES server. 请记住,上述解决方案可能会妨碍ES服务器的性能。 Read through all the posts here. 仔细阅读这里的所有帖子。

My suggestion is to use Scroll API and change the pagination style 我的建议是使用Scroll API并更改分页样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM