如何使用elasticsearch分页?从vs滚动API [英] how to do pagination with elasticsearch? from vs scroll API

查看:140
本文介绍了如何使用elasticsearch分页?从vs滚动API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Elasticsearch作为数据库来存储大量日志数据。
我知道有两种分页方法:

I'm using elasticsearch as DB to store a large batch of log data. I know there are 2 ways to do pagination:


  1. 使用大小和使用API​​

  1. Use size and from API

使用滚动API

现在我正在使用'from'进行分页。从前端和后端获取页面和大小参数(Java)

Now I'm using 'from' to do pagination.Get page and size parameters from front end,and at back end(Java)

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

但是,如果 page * size > 10000

However, if page*size > 10000, an exception thrown from ES.

我可以使用滚动API进行分页吗?

Can I use scroll API to do pagination?

我知道如果我使用滚动API, searchResponse 对象将为我返回 _scroll_id ,看起来像一个 base64 字符串。

I know that if I use scroll API, the searchResponse object will return me a _scroll_id, which looks like a base64 string.

如何控制页面和大小?

Scroll API 仅支持连续的页码吗?

It seems Scroll API only support successive page number?

推荐答案

里面没有任何内容Elasticsearch允许直接跳转到特定页面,因为必须从不同的分片中收集结果。因此,根据您的情况 search_after 将是一个更好的选择。您可以减少为后续查询返回的数据量,然后在到达实际请求的页面后获取完整数据。

There is nothing in Elasticsearch which allows direct jump to a specific page as the results have to be collected from different shards. So in your case search_after will be a better option. 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.

示例:假设您必须跳到第99页,然后可以减少所有第98页请求的数据量,在99时,您可以获得完整的数据。

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.

这篇关于如何使用elasticsearch分页?从vs滚动API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆