0

Is it possible move forward and backward using ScrollApi for pagination in ElasticSearch?

Classic solution (setFrom(), setSize()) not fit, because windows_size to small.

Code below by scrollApi work fine for infinite scroll.

if (parameters.getOffset() == 1) {
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(MY_INDEX).setTypes(documentsType);
    //...
} else {
    //
    SearchResponse scrollResponse = client.prepareSearchScroll(parameters.getScrollId())
            .setScroll(TimeValue.timeValueMinutes(1)).get();
    //...
}

2 Answers 2

0

Yes, scroll api can be used to paginate the response when the total result count is above 10000. It works this way: 1. Make first search to ES with scroll keyword and size (indicating number of results per page) 2. So ES will return you first batch of result and a scroll id. 3. You need to pass the same scroll id in the next call to ES along with size parameter to fetch next batch and so on.

If you have a front end, then you can pass scroll id in result so that the next call from UI includes that id in request and you can use it in backend service to make ES calls.

Officials site with details- https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that is what i exactly do in code above. This example work fine. But requirement is jump from page to page. Is it possible to jump from page 1 to N?
Your question said you wanted to scroll forward. Anyways looks like this post is similar, stackoverflow.com/questions/47278321/… . You might have to skip results in backend to reach a page and use search after feature to optimize.
0

You can change your design to load multiple pages worth of data in single go and return to front end, and let front end handle pagination. Lets says per page you want to show 10 records then fetch 50 records and return to front end. If user scrolls between these 5 pages then no request comes to backend. Only if user request for next set then use scroll api skip technique(I know it could still be slow) and fetch next 5 pages worth of data.

5 pages in a go

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.