use of org.elasticsearch.search.aggregations.metrics.ParsedPercentiles in project olive by ClareTung.
the class AggrMetricService method aggregationPercentiles.
/**
* percentiles 统计员工工资百分位
*/
public Object aggregationPercentiles() {
String responseResult = "";
try {
// 设置聚合条件
AggregationBuilder aggr = AggregationBuilders.percentiles("salary_percentiles").field("salary");
// 查询源构建器
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.aggregation(aggr);
searchSourceBuilder.size(0);
// 创建查询请求对象,将查询条件配置到其中
SearchRequest request = new SearchRequest("es-start-user");
request.source(searchSourceBuilder);
// 执行请求
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 获取响应中的聚合信息
Aggregations aggregations = response.getAggregations();
// 输出内容
if (RestStatus.OK.equals(response.status()) || aggregations != null) {
// 转换为 Percentiles 对象
ParsedPercentiles aggregation = aggregations.get("salary_percentiles");
log.info("-------------------------------------------");
log.info("聚合信息:");
for (Percentile percentile : aggregation) {
log.info("百分位:{}:{}", percentile.getPercent(), percentile.getValue());
}
log.info("-------------------------------------------");
}
// 根据具体业务逻辑返回不同结果,这里为了方便直接将返回响应对象Json串
responseResult = response.toString();
} catch (IOException e) {
log.error("", e);
}
return responseResult;
}
use of org.elasticsearch.search.aggregations.metrics.ParsedPercentiles in project vind by RBMHTechnology.
the class ResultUtils method getStatsFacetResults.
private static Pair<String, StatsFacetResult> getStatsFacetResults(Map<String, Aggregation> aggregations, Facet.StatsFacet statsFacet) {
final FieldDescriptor field = statsFacet.getField();
Object min = null;
Object max = null;
Double sum = null;
Long count = null;
Long missing = null;
Double sumOfSquares = null;
Object mean = null;
Double stddev = null;
Map<Double, Double> percentiles = null;
List distinctValues = null;
Long countDistinct = null;
Long cardinality = null;
final Optional<ParsedExtendedStats> statsAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName())).map(Map.Entry::getValue).map(agg -> (ParsedExtendedStats) agg).findFirst();
if (statsAggregation.isPresent() && statsFacet.getSum()) {
sum = statsAggregation.get().getSum();
}
if (statsAggregation.isPresent() && statsFacet.getMin()) {
min = DocumentUtil.castForDescriptor(statsAggregation.get().getMin(), field, FieldDescriptor.UseCase.Facet);
}
if (statsAggregation.isPresent() && statsFacet.getMax()) {
max = DocumentUtil.castForDescriptor(statsAggregation.get().getMax(), field, FieldDescriptor.UseCase.Facet);
}
if (statsAggregation.isPresent() && statsFacet.getCount()) {
count = statsAggregation.get().getCount();
}
if (statsFacet.getMissing()) {
final Optional<Aggregation> statsMissingAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName() + "_missing")).map(Map.Entry::getValue).findFirst();
if (statsMissingAggregation.isPresent()) {
missing = ((ParsedMissing) statsMissingAggregation.get()).getDocCount();
}
}
if (statsAggregation.isPresent() && statsFacet.getSumOfSquares()) {
sumOfSquares = statsAggregation.get().getSumOfSquares();
}
if (statsAggregation.isPresent() && statsFacet.getMean()) {
mean = DocumentUtil.castForDescriptor(statsAggregation.get().getAvg(), field, FieldDescriptor.UseCase.Facet);
}
if (statsAggregation.isPresent() && statsFacet.getStddev()) {
stddev = statsAggregation.get().getStdDeviation();
}
if (ArrayUtils.isNotEmpty(statsFacet.getPercentiles())) {
final Optional<Aggregation> statsPercentilesAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName() + "_percentiles")).map(Map.Entry::getValue).findFirst();
if (statsPercentilesAggregation.isPresent()) {
percentiles = Streams.stream(((ParsedPercentiles) statsPercentilesAggregation.get()).iterator()).collect(Collectors.toMap((Percentile p) -> Double.valueOf(p.getPercent()), (Percentile p) -> Double.valueOf(p.getValue())));
}
}
if (statsFacet.getDistinctValues()) {
final Optional<Aggregation> statsValuesAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName() + "_values")).map(Map.Entry::getValue).findFirst();
if (statsValuesAggregation.isPresent()) {
distinctValues = ((ParsedTerms) statsValuesAggregation.get()).getBuckets().stream().filter(bucket -> bucket.getDocCount() > 0).map(MultiBucketsAggregation.Bucket::getKey).map(o -> DocumentUtil.castForDescriptor(o, field, FieldDescriptor.UseCase.Facet)).collect(Collectors.toList());
}
}
if (statsFacet.getCountDistinct()) {
final Optional<Aggregation> statsValuesAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName() + "_values")).map(Map.Entry::getValue).findFirst();
if (statsValuesAggregation.isPresent()) {
countDistinct = ((ParsedTerms) statsValuesAggregation.get()).getBuckets().stream().filter(bucket -> bucket.getDocCount() > 0).count();
}
}
if (statsFacet.getCardinality()) {
final Optional<Aggregation> statsCardinalityAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName() + "_cardinality")).map(Map.Entry::getValue).findFirst();
if (statsCardinalityAggregation.isPresent()) {
cardinality = ((ParsedCardinality) statsCardinalityAggregation.get()).getValue();
}
}
final StatsFacetResult statsFacetResult = new StatsFacetResult(field, min, max, sum, count, missing, sumOfSquares, mean, stddev, percentiles, distinctValues, countDistinct, cardinality);
return Pair.of(statsFacet.getFacetName(), statsFacetResult);
}
Aggregations