How to use pagination with new Java Client

Trying to use a search_after with the new Java API.

That s what i did so far:

SearchResponse<PeriodInventory> sr = newClient.search(searchRequest, PeriodInventory.class);
			List<Hit<PeriodInventory>> searchHits = sr.hits().hits();

			while (searchHits != null && searchHits.size() > 0) {

				String tmp = null;
				for (Hit<PeriodInventory> searchHit : searchHits) {
					PeriodInventory pi = searchHit.source();
					if (!tmpMap.containsKey(pi.getInventory_id())) {
						tmpMap.put(pi.getInventory_id(), new AvailableBookingPeriod(pi.getInventory_id()));
					}
					tmpMap.get(pi.getInventory_id()).getPeriods().add(pi.getPeriod());
					tmp = searchHit.sort().get(0);
				}

				String lastStartDate = tmp;
				searchRequest = SearchRequest
						.of(s -> s.query(q -> q.matchAll(m -> m.queryName("all"))).index(INVENTORY_INDEX)
								.sort(sort -> sort.field(sf -> sf.field("period.start_date").order(SortOrder.Asc)))
								.searchAfter(lastStartDate).size(10000));

				sr = newClient.search(searchRequest, PeriodInventory.class);
				searchHits = sr.hits().hits();
			}

Problem is, that i only have a string value coming from the sort (date value), because searchAfter only allows string values but json api does not cause is needs a long value obviously.

How can i perform a paginated search with the new Java Client 8.1.1?

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.