In ES 5.3, you can partition the terms buckets and retrieve one partition for each request.
For example, in the query below, you can request a breakdown of your buckets into 10 sections and only return the first section. It will return ~ 10x less data than if you wanted to get all the buckets at once.
{ "size": 0, "aggs": { "my_terms": { "terms": { "field": "my_field", "include": { "partition": 0, "num_partitions": 10 }, "size": 10000 } } } }
Then you can make a second request by increasing the section to 1, etc.
{ "size": 0, "aggs": { "my_terms": { "terms": { "field": "my_field", "include": { "partition": 1, <--- increase this up until partition 9 "num_partitions": 10 }, "size": 10000 } } } }
To add this to your Java code, you can do it like this:
TermsAggregationBuilder agg = AggregationBuilders.terms("my_terms"); agg.includeExclude(new IncludeExclude(0, 10));
source share