Wijayanga Wijekoon
Wijayanga Wijekoon

Reputation: 99

Elasticsearch aggregation buckets always showing only 10 results

I'm using Elasticsearch aggregation for load data, when passing offset count the response results are showing correctly but terms aggregation results/buckets always showing only 10 results.

Map<String, BucketStats> hourAggregations = new HashMap<>();
    SearchResponse getResponse = client.prepareSearch( ElasticSearchConstants.INDEX ).setTypes( ElasticSearchConstants.TABLE)
            .addAggregation( AggregationBuilders.terms( "name")
                    .field( "empId" )
                    .subAggregation( AggregationBuilders.stats( "stats" )
            .setQuery( query )
            .setFrom( 0 )
            .setSize( ( offset + 1 ) * 10 )
            .execute().actionGet();

    Terms terms = getResponse.getAggregations().get("name");
    for ( Terms.Bucket bucket : terms.getBuckets() )
    {
    //buckets showing only 10 results
    }

Upvotes: 0

Views: 452

Answers (1)

Val
Val

Reputation: 217304

You need to give a size to your terms aggregation:

Map<String, BucketStats> hourAggregations = new HashMap<>();
SearchResponse getResponse = client.prepareSearch( ElasticSearchConstants.INDEX ).setTypes( ElasticSearchConstants.TABLE)
        .addAggregation( AggregationBuilders.terms( "name")
                .field( "empId" )
-->             .size(100)
                .subAggregation( AggregationBuilders.stats( "stats" )
        .setQuery( query )
        .setFrom( 0 )
        .setSize( ( offset + 1 ) * 10 )
        .execute().actionGet();

The setSize() call only impacts the hits section, i.e. the document set in the results, not the aggregations. The way I see it you should call setSize(0), because you only want aggregation values and not documents.

Upvotes: 1

Related Questions