Java search API pagination question/issue


(jwray) #1

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


(Shay Banon) #2

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/.
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 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


(jwray) #3

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 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....
It is better to use query_then_fetch, which is the default.

-shay.banon


(system) #4