Search code examples
elasticsearchelasticsearch-jest

Jest Client Size Parameter Ignored


I am using the Jest Client to query Elasticsearch from my Java program. Everything works correctly except that when I add the "size" parameter it is ignored. Building and execution of the Search is like so:

Search search = new Search.Builder(query)
    .setSearchType(SearchType.QUERY_THEN_FETCH)
    .addIndex(index)
    .addType(type)
    .setParameter(Parameters.SIZE, 1)
    .build();
jestClient.execute(search);

This query always returns 10 results, instead of the 1 result expected. In case it is relevant, there are only 5 shards, so it is not returning a result per shard.

Is there any particular reason this parameter is ignored? When running the same query with the same parameters on the command line with 'curl -XGET' or when simply putting it in the browser the query runs correctly and the size parameter is taken in to account.


Solution

  • It turns out I had a misunderstanding of how the size parameter was working. I believe Jest was handling it correctly when sizing the query results, but what I was actually interested in were the aggregation results.

    Adding a "size" parameter to my aggregation worked, as can be seen in the Terms Aggregation docs (copied below).

    {
        "aggs" : {
            "products" : {
                "terms" : {
                    "field" : "product",
                    "size" : 5
                }
            }
        }
    }