# Pagination using the java api

**URL:** <https://discuss.elastic.co/t/pagination-using-the-java-api/98675>\
**Category:** Elasticsearch\
**Created:** [August 29, 2017, 11:27am UTC](https://discuss.elastic.co/t/pagination-using-the-java-api/98675 "2017-08-29T11:27:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![YvesC](https://avatars.discourse-cdn.com/v4/letter/y/f6c823/32.png) [@YvesC](https://discuss.elastic.co/u/YvesC)\
**Post date:** [August 29, 2017, 11:27am UTC](https://discuss.elastic.co/t/pagination-using-the-java-api/98675/1 "2017-08-29T11:27:04Z")

</div>

Hi all,  
I am currently in the progress of migrating our code to be compatible with ES 5.4 (we came from 2.3)  
I am using the Java API as described in the elasticsearch documentation (5.4) but came across something weird.  
So the start situation in 2.3 was this query:

> ```
> String searchString = "{\n" +
> " \"from\": <START_FROM>,\n" +
> " \"size\": <RESULTS_SIZE>,\n" +
> " \"query\": {\n" +
> " \"term\": {\n" +
> " \"" + fieldName + "\": \"" + fieldValue + "\"\n" +
> " }\n" +
> " }\n" +
> "}";
> 
> ```

So from and size were replaced and we had a working query.  
However I am currently using the wrapperQueryBuilder as per the documentation of 5.4.  
So I get the following statement:

> SearchResponse result = this.client.prepareSearch(index).setSource(new SearchSourceBuilder().query(new WrapperQueryBuilder(finalString))).get();

The query also had to be rewritten as follows (note the missing keywords query, from , size):

> ```
> private String getSearchString(String fieldName, String fieldValue){
> String searchString = "{\n" +
> " \"term\": {\n" +
> " \"" + fieldName + "\": \"" + fieldValue + "\"\n" +
> " }\n" +
> "}";
> return searchString;
> }
> String finalString = searchString.replaceAll("<START_FROM>", String.valueOf(startFrom)).replaceAll("<RESULTS_SIZE>", String.valueOf(resultsSize));
> 
> ```

Now my question is, how do you pagination with the Java WrapperQueryBuilder / SearchSourceBuilder / SearchRequestBuilder?  
I discovered that SearchSourceBuilder has a method **setFrom(int)**, but is there a way to set the size for the result ? I've been looking but can't seem to find it.

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![YvesC](https://avatars.discourse-cdn.com/v4/letter/y/f6c823/32.png) [@YvesC](https://discuss.elastic.co/u/YvesC)\
**Post date:** [August 30, 2017, 8:26am UTC](https://discuss.elastic.co/t/pagination-using-the-java-api/98675/2 "2017-08-30T08:26:42Z")

</div>

Nevermind, found it 🙂

---

<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 27, 2017, 8:26am UTC](https://discuss.elastic.co/t/pagination-using-the-java-api/98675/3 "2017-09-27T08:26:43Z")

</div>

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