# How to use pagination with new Java Client

**URL:** https://discuss.elastic.co/t/how-to-use-pagination-with-new-java-client/339460
**Category:** Elasticsearch
**Created:** [July 27, 2023, 4:42pm UTC](https://discuss.elastic.co/t/how-to-use-pagination-with-new-java-client/339460 "2023-07-27T16:42:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Andre\_Schmer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andre_schmer/32/122097_2.png) [@Andre\_Schmer](https://discuss.elastic.co/u/Andre_Schmer)
#### Post date: [July 27, 2023, 4:42pm UTC](https://discuss.elastic.co/t/how-to-use-pagination-with-new-java-client/339460/1 "2023-07-27T16:42:24Z")

</div>

Trying to use a search\_after with the new Java API.

That s what i did so far:

```auto
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?

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [August 24, 2023, 4:42pm UTC](https://discuss.elastic.co/t/how-to-use-pagination-with-new-java-client/339460/2 "2023-08-24T16:42:47Z")

</div>

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