valuecount-aggregation.asciidoc 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. [[java-aggs-metrics-valuecount]]
  2. ==== Value Count Aggregation
  3. Here is how you can use
  4. {ref}/search-aggregations-metrics-valuecount-aggregation.html[Value Count 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. ValueCountAggregationBuilder aggregation =
  11. AggregationBuilders
  12. .count("agg")
  13. .field("height");
  14. --------------------------------------------------
  15. ===== Use aggregation response
  16. Import Aggregation definition classes:
  17. [source,java]
  18. --------------------------------------------------
  19. import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount;
  20. --------------------------------------------------
  21. [source,java]
  22. --------------------------------------------------
  23. // sr is here your SearchResponse object
  24. ValueCount agg = sr.getAggregations().get("agg");
  25. long value = agg.getValue();
  26. --------------------------------------------------