# How to implement pagination using java

**URL:** <https://discuss.elastic.co/t/how-to-implement-pagination-using-java/246018>\
**Category:** Elasticsearch\
**Created:** [August 23, 2020, 12:51pm UTC](https://discuss.elastic.co/t/how-to-implement-pagination-using-java/246018 "2020-08-23T12:51:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhishek\_chaturvedi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_chaturvedi/32/74365_2.png) [@abhishek\_chaturvedi](https://discuss.elastic.co/u/abhishek_chaturvedi)\
**Post date:** [August 23, 2020, 12:51pm UTC](https://discuss.elastic.co/t/how-to-implement-pagination-using-java/246018/1 "2020-08-23T12:51:38Z")

</div>

Hi All,

## Could someone guide me with some code snippet on writing pagination logic using ES java rest high level client api. Below is my service code:

```
@Override
	public List getResponses(ZonedDateTime startDate, ZonedDateTime endDate, String cat, FieldFilterVM filter, String query) throws IOException {
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Request request = new Request("GET", "/" + user.getUserId() + "/_search");
		List<String> matchQueries = new ArrayList<>();
		matchQueries.addAll(formatCategoryQuery(cat));
		matchQueries.addAll(formatFilterQuery(filter, false));
		if (query != null && query.length() > 0) {
			matchQueries.add(String.format(textFilterQuery, query));
		}
		StringBuilder formattedQueries = new StringBuilder();
		for (int i = 0; i < matchQueries.size(); i++) {
			formattedQueries.append(',');
			formattedQueries.append(matchQueries.get(i));
		}
		String esQuery = String.format(searchTextQuery, startDate, endDate, formattedQueries);
		request.setJsonEntity(esQuery);
		Response response = elasticSearchService.getClient().getLowLevelClient().performRequest(request);
		String responseBody = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
		ObjectMapper mapper = new ObjectMapper();
		Map map = mapper.readValue(responseBody, new TypeReference<Map>() {
		});
		List matchedTextResponses = new ArrayList();
		if (map != null) {
			List<Map> textResponses = (List<Map>) ((Map) map.get("hits")).get("hits");
			for (Map textResponse : textResponses) {
				
			}
		}
		return matchedTextResponses;
	}

```

* * *

---

<div class="post-metadata">

**Author:** ![wangqinghuan](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@wangqinghuan](https://discuss.elastic.co/u/wangqinghuan)\
**Post date:** [August 23, 2020, 1:34pm UTC](https://discuss.elastic.co/t/how-to-implement-pagination-using-java/246018/2 "2020-08-23T13:34:55Z")

</div>

You are using ES java rest low client api. You should use High Level Rest client which can provide specific API, for example, pagination.

> SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();  
> sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy"));  
> sourceBuilder.from(0);  
> sourceBuilder.size(5);  
> sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

---

<div class="post-metadata">

**Author:** ![abhishek\_chaturvedi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_chaturvedi/32/74365_2.png) [@abhishek\_chaturvedi](https://discuss.elastic.co/u/abhishek_chaturvedi)\
**Post date:** [August 23, 2020, 3:34pm UTC](https://discuss.elastic.co/t/how-to-implement-pagination-using-java/246018/3 "2020-08-23T15:34:05Z")

</div>

Thanks for the reply.  
Is there any way to use ElasticsearchRepository which extends from PagingAndSortingRepository. This allows built-in support for pagination and sorting.  
But I am not able to change my implementation to use ElasticsearchRepository. I just want to know how to apply:

1. How to use Post Search
2. How to use esQuery which basically providing Search query  
String esQuery = String.format(searchTextQuery, startDate, endDate, formattedQueries);  
3 How to use that Post URI which I m getting as below:  
Request request = new Request("GET", "/" + user.getUserId() + "/\_search");  
So with all above how to use Pagination with ElasticsearchRepository

---

<div class="post-metadata">

**Author:** ![wangqinghuan](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@wangqinghuan](https://discuss.elastic.co/u/wangqinghuan)\
**Post date:** [August 24, 2020, 1:26am UTC](https://discuss.elastic.co/t/how-to-implement-pagination-using-java/246018/4 "2020-08-24T01:26:00Z")

</div>

> [@abhishek\_chaturvedi](#):
>
> ElasticsearchRepository

ElasticsearchRepository is a spring data project. You will get more helpful info from Spring commutity.

---

<div class="post-metadata">

**Author:** ![abhishek\_chaturvedi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_chaturvedi/32/74365_2.png) [@abhishek\_chaturvedi](https://discuss.elastic.co/u/abhishek_chaturvedi)\
**Post date:** [August 24, 2020, 6:59am UTC](https://discuss.elastic.co/t/how-to-implement-pagination-using-java/246018/5 "2020-08-24T06:59:32Z")

</div>

Ok but I have tried with High Level Rest client and with that kindly reply for below 2 points.

1. How to use pagination like here can see we have "from" and "size" parameters but what about "to". Without "to" how to navigate backwards. With "from" we can navigate forward a page and can't move to the backward page. Suppose we are in Page 10 and now want to move directly to the 4th page then how to move it.
2. With HighlevelClient:  
Below is the implementation code:

```auto
private final RestHighLevelClient esClient;
SearchResponse response = esClient.search(new SearchRequest("0")
			.source(new SearchSourceBuilder()
				.query(QueryBuilders.wrapperQuery(esQuery))
				.from(0)
				.size(5)
				.trackTotalHits(true)
				.sort(SortBuilders.scoreSort())
				.sort(SortBuilders.fieldSort("title"))
			), RequestOptions.DEFAULT);

```

And with above code getting below exception:

method [POST], host [[http://localhost:9200](http://localhost:9200)], URI [/0/\_search?typed\_keys=true&ignore\_unavailable=false&expand\_wildcards=open&allow\_no\_indices=true&search\_type=query\_then\_fetch&batched\_reduce\_size=512], status line [HTTP/1.1 400 Bad Request]  
{"error":{"root\_cause":[{"type":"parsing\_exception","reason":"[size] query malformed, no start\_object after query name","line":2,"col":11}],"type":"parsing\_exception","reason":"[size] query malformed, no start\_object after query name","line":2,"col":11},"status":400}

Below is my query and getting result when trying with Low-Level Rest client and also working when using in Kibana tool:  
POST /0/\_search/  
{  
"size": 1000,  
"query": {  
"bool": {  
"must": [  
{  
"nested": {  
"path": "history",  
"query": {  
"range": {  
"history.responseDate": {  
"gte": "2019-12-31T18:30Z",  
"lte": "2020-12-31T18:29:59.999Z"  
}  
}  
}  
}  
},  
{  
"nested": {  
"path": "history",  
"query": {  
"nested": {  
"path": "history.sections",  
"query": {  
"nested": {  
"path": "history.sections.historyquess",  
"query": {  
"nested": {  
"path": "history.sections.historyquess.ques",  
"query": {  
"nested": {  
"path": "history.sections.historyquess.ques.ans",  
"query": {  
"exists": {  
"field": "history.sections.historyquess.ques.ans.other"  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
]  
}  
},  
"\_source": [  
"history.sections.historyquess.ques.ans.other",  
"history.sections.historyquess.ques.title"  
]  
}

Thanks in advance.

---

<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:** [September 21, 2020, 6:59am UTC](https://discuss.elastic.co/t/how-to-implement-pagination-using-java/246018/6 "2020-09-21T06:59:35Z")

</div>

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