# Java search API pagination question/issue

**URL:** <https://discuss.elastic.co/t/java-search-api-pagination-question-issue/3455>\
**Category:** Elasticsearch\
**Created:** [October 19, 2010, 6:03pm UTC](https://discuss.elastic.co/t/java-search-api-pagination-question-issue/3455 "2010-10-19T18:03:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwray](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jwray/32/2837_2.png) [@jwray](https://discuss.elastic.co/u/jwray)\
**Post date:** [October 19, 2010, 6:03pm UTC](https://discuss.elastic.co/t/java-search-api-pagination-question-issue/3455/1 "2010-10-19T18:03:33Z")

</div>

Hi,

I've just started playing with Elastic Search and I'm very impressed.  
I've been a compass users for a number of years and I'm starting to  
like ES even more. Great job, and thanks.

So, I've managed to get up and running and got my data indexed but  
experiencing unexpected behaviour with the Java search API. Here's my  
query:

String queryString = "FGFR\*";  
SearchRequestBuilder builder = client.prepareSearch("sherlock");  
builder.setSearchType(SearchType.QUERY\_AND\_FETCH).setQuery(queryString(queryString));  
builder.setFrom(0).setSize(10);  
SearchResponse response = builder.execute().actionGet();  
System.out.println("Response "+response.hits().hits().length);  
System.out.println("Response: "+response.getHits().getTotalHits());  
System.out.println("Response: "+response.hits().getHits().length);

I'd expect the setFrom and setSize to limit the hits array to length  
10, but I'm getting all of them (same as getTotalHits(), which is 25  
in my test case). If I change the from parameter to, say 5, I now get  
2 for the length of the hit array. In both cases I'd expect an array  
of length 10.

So, in short, am I miss understanding the purpose of from and size,  
and if so how do I implement pagination with the Java API, or have I  
hit on a bug?

thanks  
Jonny

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 19, 2010, 6:42pm UTC](https://discuss.elastic.co/t/java-search-api-pagination-question-issue/3455/2 "2010-10-19T18:42:51Z")

</div>

Hi,

The query\_and\_fetch returns size \* number\_of\_shards hits back, explanation  
is here:  
[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/search/search\_type/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/search/search_type/).  
It is better to use query\_then\_fetch, which is the default.

-shay.banon

On Tue, Oct 19, 2010 at 8:03 PM, jwray [jonny.wray@gmail.com](mailto:jonny.wray@gmail.com) wrote:

> Hi,
> 
> I've just started playing with Elastic Search and I'm very impressed.  
> I've been a compass users for a number of years and I'm starting to  
> like ES even more. Great job, and thanks.
> 
> So, I've managed to get up and running and got my data indexed but  
> experiencing unexpected behaviour with the Java search API. Here's my  
> query:
> 
> String queryString = "FGFR\*";  
> SearchRequestBuilder builder = client.prepareSearch("sherlock");
> 
> builder.setSearchType(SearchType.QUERY\_AND\_FETCH).setQuery(queryString(queryString));  
> builder.setFrom(0).setSize(10);  
> SearchResponse response = builder.execute().actionGet();  
> System.out.println("Response "+response.hits().hits().length);  
> System.out.println("Response: "+response.getHits().getTotalHits());  
> System.out.println("Response: "+response.hits().getHits().length);
> 
> I'd expect the setFrom and setSize to limit the hits array to length  
> 10, but I'm getting all of them (same as getTotalHits(), which is 25  
> in my test case). If I change the from parameter to, say 5, I now get  
> 2 for the length of the hit array. In both cases I'd expect an array  
> of length 10.
> 
> So, in short, am I miss understanding the purpose of from and size,  
> and if so how do I implement pagination with the Java API, or have I  
> hit on a bug?
> 
> thanks  
> Jonny

---

<div class="post-metadata">

**Author:** ![jwray](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jwray/32/2837_2.png) [@jwray](https://discuss.elastic.co/u/jwray)\
**Post date:** [October 19, 2010, 6:55pm UTC](https://discuss.elastic.co/t/java-search-api-pagination-question-issue/3455/3 "2010-10-19T18:55:41Z")

</div>

Thanks for the prompt reply. I knew I had to be missing something  
obvious.

Jonny

On Oct 19, 11:42 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Hi,
> 
> The query\_and\_fetch returns size \* number\_of\_shards hits back, explanation  
> is here:[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/search/searc](http://www.elasticsearch.com/docs/elasticsearch/rest_api/search/searc)....  
> It is better to use query\_then\_fetch, which is the default.
> 
> -shay.banon

---

<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:** [July 6, 2017, 4:17am UTC](https://discuss.elastic.co/t/java-search-api-pagination-question-issue/3455/4 "2017-07-06T04:17:49Z")

</div>


