Issue
We have a load more button on UI which requests for next pages. We have used SearchAfter query param to implement ES Pagination
I am using sortValues present in ElasticSearch Response to generate nextPageToken.
I found a scenario where even though next page didn't exists, I am getting sort values in Elastic search response and when I queries ES with that sort values it returns empty response.
What should I do to avoid this scenario? Can I determine in advance whether next page exists or not?
Another thing coming into my mind is can we assume if ES response size is less than requested size means we don't have any new pages?
//SearchRequest code snippet
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
.query(query)
.fetchSource(getSessionsQuery.getColumns().toArray(String[]::new), null)
.size(getSessionsQuery.getResultSize())
.sort(new FieldSortBuilder(route.getTechnologyConfiguration().get(TIMESTAMP_FIELD_KEY)).order(SortOrder.ASC))
.sort(new FieldSortBuilder(route.getTechnologyConfiguration().get(VERSION_SORT_KEY)).order(SortOrder.DESC));
if (!StringUtils.isNullOrEmpty(getSessionsQuery.getNextToken())) {
final String[] tokens = getSessionsQuery.getNextToken().split(ES_SORT_FIELD_DELIMITER);
if (tokens.length > 0) {
sourceBuilder.searchAfter(tokens);
}
}
return new SearchRequest().indices(route.getTechnologyConfiguration().get(INDEX_KEY)).source(sourceBuilder);
//code snippet to generate next page token
if (hits[hits.length - 1].getSortValues().length > 0) {
final String esSortFieldToken = Joiner.on(ES_SORT_FIELD_DELIMITER).join(hits[hits.length - 1].getSortValues());
nextPageToken = Joiner.on(NEXT_PAGE_TOKEN_DELIMITER).join(route.getRouteName(), esSortFieldToken);
}
Solution
Yes, you can assume that there are no more pages when you get less than size
(or zero) hits back.
Answered By - ilvar Answer Checked By - Gilberto Lyons (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.