How to implement pagination using java

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;
	}

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));

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

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

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:
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], 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.

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