percentile-aggregation.asciidoc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. [[java-aggs-metrics-percentile]]
  2. ==== Percentile Aggregation
  3. Here is how you can use
  4. {ref}/search-aggregations-metrics-percentile-aggregation.html[Percentile 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. PercentilesAggregationBuilder aggregation =
  11. AggregationBuilders
  12. .percentiles("agg")
  13. .field("height");
  14. --------------------------------------------------
  15. You can provide your own percentiles instead of using defaults:
  16. [source,java]
  17. --------------------------------------------------
  18. PercentilesAggregationBuilder aggregation =
  19. AggregationBuilders
  20. .percentiles("agg")
  21. .field("height")
  22. .percentiles(1.0, 5.0, 10.0, 20.0, 30.0, 75.0, 95.0, 99.0);
  23. --------------------------------------------------
  24. ===== Use aggregation response
  25. Import Aggregation definition classes:
  26. [source,java]
  27. --------------------------------------------------
  28. import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
  29. import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
  30. --------------------------------------------------
  31. [source,java]
  32. --------------------------------------------------
  33. // sr is here your SearchResponse object
  34. Percentiles agg = sr.getAggregations().get("agg");
  35. // For each entry
  36. for (Percentile entry : agg) {
  37. double percent = entry.getPercent(); // Percent
  38. double value = entry.getValue(); // Value
  39. logger.info("percent [{}], value [{}]", percent, value);
  40. }
  41. --------------------------------------------------
  42. This will basically produce for the first example:
  43. [source,text]
  44. --------------------------------------------------
  45. percent [1.0], value [0.814338896154595]
  46. percent [5.0], value [0.8761912455821302]
  47. percent [25.0], value [1.173346540141847]
  48. percent [50.0], value [1.5432023318692198]
  49. percent [75.0], value [1.923915462033674]
  50. percent [95.0], value [2.2273644908535335]
  51. percent [99.0], value [2.284989339108279]
  52. --------------------------------------------------