Paginating browse or browseObjects should only be used for non-front end use cases (e.g. backups).
Don't use getPage for this. Instead, use an iterator. If you are unfamiliar with iterators, they are a mechanism for making multiple browse requests. You just need to ensure that you include a way for the browse requests to end.
For example, with a pagination size of 4 items, you might do something like this:
IndexIterable<Hit> iterable = index.browseObjects(new BrowseIndexQuery().setHitsPerPage(5));
List<Hit> hits = new ArrayList<>();
for (Hit hit : iterable) {
hits.add(hit);
if (hits.size() == 4) break; // the will iterate until you get the first 4 items
}
System.out.println(hits.size());
An alternative way to do this is using streams:
IndexIterable<Hit> iterable = index.browseObjects(new BrowseIndexQuery().setHitsPerPage(5));
List<Hit> hits = StreamSupport.stream(iterable.spliterator(), false) // this converts the iterator into a stream
.limit(4) // this will get the first N items
.collect(Collectors.toList()); // collect each N items into a list
System.out.println(hits.size());