I'm trying to test pagination in the latest release (8.5) with point-in-time and search after in the Java API. I don't know exactly how to implement it as the Java API documentation is barely existent and the only reference being the elasticsearch one. I've tried this:
public List<Offer> searchOffer(OfferRequest request) throws IOException {
List<Offer> offers=new ArrayList<>();
Query query = prepareSearchElasticQuery(request);
PointInTimeReference.Builder pit=new PointInTimeReference.Builder();
pit.keepAlive(new Time.Builder().time("1m").build());
SearchResponse<Offer> response = elasticsearchClient.search(s -> s
.index("offers")
.pit(pit.build())
.searchAfter()
.size(1000)
.query(query)
.sort(new SortOptions.Builder().field(f>f.field("offerId").order(SortOrder.Asc))),
Offer.class
);
But I'm not sure if it's the correct way to build it, and also what argument should searchAfter() take.
Edit: I ran a query on the first elements before the page I want and got the id of the last document. Then I passed it to the searchAfter method:
SearchResponse<Offer> response = elasticsearchClient.search(s -> s
.index("offers")
.size(PAGE_SIZE)
.searchAfter(sa->sa.stringValue(searchAfterId))
.query(query)
.sort(new SortOptions.Builder()
.field(f-> f.field("offerId").order(SortOrder.Asc))
.build())
,Offer.class
);
CodePudding user response:
The searchAfter receive list parameters from your Sort. I see you use "offerId" to sort results, so you must put inside searchAfter the last value sort of response hits.