histogram-aggregation.asciidoc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. [[java-aggs-bucket-histogram]]
  2. ==== Histogram Aggregation
  3. Here is how you can use
  4. {ref}/search-aggregations-bucket-histogram-aggregation.html[Histogram 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. AggregationBuilder aggregation =
  11. AggregationBuilders
  12. .histogram("agg")
  13. .field("height")
  14. .interval(1);
  15. --------------------------------------------------
  16. ===== Use aggregation response
  17. Import Aggregation definition classes:
  18. [source,java]
  19. --------------------------------------------------
  20. import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
  21. --------------------------------------------------
  22. [source,java]
  23. --------------------------------------------------
  24. // sr is here your SearchResponse object
  25. Histogram agg = sr.getAggregations().get("agg");
  26. // For each entry
  27. for (Histogram.Bucket entry : agg.getBuckets()) {
  28. Number key = (Number) entry.getKey(); // Key
  29. String keyAsString = entry.getKeyAsString(); // Key As String
  30. long docCount = entry.getDocCount(); // Doc count
  31. }
  32. --------------------------------------------------