23

Is there a way to retrieve the aggregations' buckets in a search response, with the java API ?

{
  "took" : 185,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 200,
    "max_score" : 1.0,
    "hits" : [...]
  },
  "aggregations" : {
    "agg1" : {
      "buckets" : [...]
    },
    "agg2" : {
      "buckets" : [...]
    }
  }
}

Currently, it's possible to get the aggregations but I can't figure out how to get the buckets.

Current 1.0 version of ElasticSearch (v1.0.0.Beta2) is still a beta version, and maybe this feature still has to be added, but didn't find info on that point too.

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
ThomasC
  • 7,915
  • 2
  • 26
  • 26

3 Answers3

41

Looking at the ES source on Github I see the following in their tests:

SearchResponse response = client().prepareSearch("idx").setTypes("type")
                .setQuery(matchAllQuery())
                .addAggregation(terms("keys").field("key").size(3).order(Terms.Order.count(false)))
                .execute().actionGet();

Terms  terms = response.getAggregations().get("keys");
Collection<Terms.Bucket> buckets = terms.getBuckets();
assertThat(buckets.size(), equalTo(3));
mconlin
  • 8,169
  • 5
  • 31
  • 37
  • Great! This is exactly the missing "link" I was looking for, I hope there will be something in the API or the reference to explain this part. – ThomasC Jan 09 '14 at 12:31
  • 1
    I think you have now to use `terms.getBuckets()` instead of `terms.buckets()`. – Sonson123 Apr 17 '14 at 11:49
  • 2
    How is this answer the correct one? Terms.Bucket is package protected and can't be accessed. – Michael May 22 '14 at 17:03
  • I see Terms.Bucket is not package protected; however, DateHistogram.Bucket is. To get DateHistogram buckets you would have to use: Collection extends DateHistogram.Bucket> buckets = dateHistogram.getBuckets(); This is what was confusing me. – Michael May 23 '14 at 14:56
  • If you are trying to get buckets of Significant String Terms aggregation : `Collection buckets = ((SignificantStringTerms) response.getAggregations().get("keys")).getBuckets();` – chill appreciator May 23 '20 at 23:07
9

If anyone wonder about accessing actual documents count out of these buckets following code might help.

Terms  terms = response.getAggregations().get("agg1");
Collection<Terms.Bucket> buckets = terms.getBuckets();
for (Bucket bucket : buckets) {
    System.out.println(bucket.getKeyAsText() +" ("+bucket.getDocCount()+")");
}
Charith De Silva
  • 3,650
  • 4
  • 43
  • 47
0

In case anyone is looking for a sum aggregation, it's as easy as assigning the result to a org.elasticsearch.search.aggregations.metrics.sum.Sum variable:

ElasticConnector connector = ... // initialize ES connector
SearchResponde response = connector.getClient()
    .prepareSearch(...)
    .setQuery(...)
    .addAggregation(AggregationBuilders.sum("my_sum").field(...))
    .get();
Sum sum = response.getAggregations().get("my_sum");
double result = sum.getValue();
Gustavo Passini
  • 2,348
  • 19
  • 25