PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 24, 2022

[FIXED] How to determine if next page exists with searchAfter query param of ElasticSearchRequest

 October 24, 2022     elasticsearch, elasticsearch-5, opensearch     No comments   

Issue

  1. We have a load more button on UI which requests for next pages. We have used SearchAfter query param to implement ES Pagination

  2. I am using sortValues present in ElasticSearch Response to generate nextPageToken.

  3. 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.

  4. What should I do to avoid this scenario? Can I determine in advance whether next page exists or not?

  5. 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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing