global-aggregation.asciidoc 991 B

1234567891011121314151617181920212223242526272829303132333435
  1. [[java-aggs-bucket-global]]
  2. ==== Global Aggregation
  3. Here is how you can use
  4. {ref}/search-aggregations-bucket-global-aggregation.html[Global Aggregation]
  5. with Java API.
  6. ===== Prepare aggregation request
  7. Here is an example on how to create the aggregation request:
  8. [source,java]
  9. --------------------------------------------------
  10. AggregationBuilders
  11. .global("agg")
  12. .subAggregation(AggregationBuilders.terms("genders").field("gender"));
  13. --------------------------------------------------
  14. ===== Use aggregation response
  15. Import Aggregation definition classes:
  16. [source,java]
  17. --------------------------------------------------
  18. import org.elasticsearch.search.aggregations.bucket.global.Global;
  19. --------------------------------------------------
  20. [source,java]
  21. --------------------------------------------------
  22. // sr is here your SearchResponse object
  23. Global agg = sr.getAggregations().get("agg");
  24. agg.getDocCount(); // Doc count
  25. --------------------------------------------------