org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 6 votes vote down vote up
private static PercentilesResult reducePercentilesResults(ElasticsearchSearchQueryBase query, List<Aggregation> aggs) {
    List<Percentile> results = new ArrayList<>();
    if (aggs.size() != 1) {
        throw new VertexiumException("Unexpected number of aggregations. Expected 1 but found: " + aggs.size());
    }
    Aggregation agg = aggs.get(0);
    if (agg instanceof Percentiles) {
        Percentiles percentiles = (Percentiles) agg;
        StreamUtils.stream(percentiles)
            .filter(percentile -> !Double.isNaN(percentile.getValue()))
            .forEach(percentile -> results.add(new Percentile(percentile.getPercent(), percentile.getValue())));
    } else {
        throw new VertexiumException("Aggregation is not a percentile: " + agg.getClass().getName());
    }
    return new PercentilesResult(results);
}
 
Example #2
Source File: SearchServiceImpl.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
/**
 *
 *对指定字段(脚本)的值按从小到大累计每个值对应的文档数的占比(占所有命中文档数的百分比),返回指定占比比例对应的值。
 * 默认返回[ 1, 5, 25, 50, 75, 95, 99 ]分位上的值。如下中间的结果,可以理解为:占比为50%的文档的age值 <= 31,
 * 或反过来:age<=31的文档数占总命中文档数的50%
 *  也可以自己指定分位值,如 10,50,90.
 * @param hostIp         ES集群的ip地址
 * @param port ES集群的端口号
 * @param clusterName  ES集群名称
 * @param indexName  ES集群的索引名称,可使用多个索引 indexName="test2,test1,test";
 * @param typeName 索引类型,可多个 typeName="doc,pdf,test";
 * @param aggFdName 字段名称
 * @param percentiles 自定义百分比分位数,比如10,50,90 之间以","分隔
 * @return
 * @throws TException
 */
@Override
public Map<String, String> PercentilesAggregation(String hostIp, int port, String clusterName, String indexName, String typeName, String aggFdName, String percentiles) throws TException {
    Client client=null;
    try {
        client = ESUtils.getClient( hostIp, port, clusterName );
    } catch (Exception e) {
        e.printStackTrace();
    }
    PercentilesAggregationBuilder aggregation=null;
    if(percentiles==null&&percentiles.equals( "" )) {
        aggregation =
                AggregationBuilders.percentiles( aggFdName + "_percentiles" ).field( aggFdName );
    }else {
        double[] parseDouble=new double[10];
        if(percentiles.contains( "," )){
            String[] split = percentiles.split( "," );
            for (int i = 0; i <split.length ; i++) {
                parseDouble[i] = Double.parseDouble( split[i] );
            }
        }else {
            parseDouble=new double[]{Double.parseDouble( percentiles )};
        }
        aggregation =
                AggregationBuilders.percentiles( aggFdName + "_percentiles" ).field( aggFdName ).percentiles(  parseDouble  );
    }
    SearchResponse sr = client.prepareSearch(indexName)
                                .addAggregation(aggregation)
                                .get();
    Percentiles percentiles1 = sr.getAggregations().get( aggFdName+"_percentiles" );
    Map map=new LinkedHashMap(  );
    for (Percentile percentile:percentiles1) {
        System.out.println("global " + percentile.getPercent() + " count:" + percentile.getValue());
        map.put( String.valueOf(percentile.getPercent()+"%" ),String.valueOf(aggFdName+"="+ percentile.getValue()) );
    }
    return map;
}
 
Example #3
Source File: StatsTrendAction.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
private List<StatsTrendValue> buildStatsTrendValue(String field, Aggregations aggregations) {
    String dateHistogramKey = Utils.getDateHistogramKey(getParameter().getTimestamp());
    Histogram dateHistogram = aggregations.get(dateHistogramKey);
    Collection<? extends Histogram.Bucket> buckets = dateHistogram.getBuckets();
    List<StatsTrendValue> statsValueList = Lists.newArrayList();

    final String metricKey = Utils.getExtendedStatsAggregationKey(field);
    final String percentileMetricKey = Utils.getPercentileAggregationKey(field);

    for (Histogram.Bucket bucket : buckets) {
        StatsTrendValue statsTrendValue = new StatsTrendValue();
        DateTime key = (DateTime) bucket.getKey();
        statsTrendValue.setPeriod(key.getMillis());

        val statAggregation = bucket.getAggregations()
                .getAsMap()
                .get(metricKey);
        statsTrendValue.setStats(Utils.toStats(statAggregation));
        final Aggregation rawPercentiles = bucket.getAggregations()
                .getAsMap()
                .get(percentileMetricKey);
        if (null != rawPercentiles) {
            Percentiles internalPercentile = Percentiles.class.cast(rawPercentiles);
            statsTrendValue.setPercentiles(Utils.createPercentilesResponse(internalPercentile));
        }
        statsValueList.add(statsTrendValue);
    }
    return statsValueList;
}
 
Example #4
Source File: StatsAction.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
private static StatsValue buildStatsValue(String field, Aggregations aggregations) {
    String metricKey = Utils.getExtendedStatsAggregationKey(field);
    String percentileMetricKey = Utils.getPercentileAggregationKey(field);

    // Build top level stats
    StatsValue statsValue = new StatsValue();
    statsValue.setStats(Utils.toStats(aggregations.getAsMap()
                                              .get(metricKey)));
    Percentiles internalPercentile = (Percentiles) aggregations.getAsMap()
            .get(percentileMetricKey);
    if (null != internalPercentile) {
        statsValue.setPercentiles(Utils.createPercentilesResponse(internalPercentile));
    }
    return statsValue;
}
 
Example #5
Source File: Utils.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
public static Map<Number, Number> createPercentilesResponse(Percentiles internalPercentiles) {
    Map<Number, Number> percentiles = Maps.newHashMap();
    for (Percentile percentile : internalPercentiles) {
        percentiles.put(percentile.getPercent(), percentile.getValue());
    }
    return percentiles;
}
 
Example #6
Source File: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
private void evaluateDoubleEstimation(Aggregation value, String table, String key, FieldType type,
                                      Map<String, EstimationData> estimationDataMap, long hits) {
    if(value instanceof Percentiles) {
        Percentiles percentiles = (Percentiles)value;
        double[] values = new double[10];
        for(int i = 10; i <= 100; i += 10) {
            final Double percentile = percentiles.percentile(i);
            values[(i / 10) - 1] = percentile.isNaN() ? 0 : percentile;
        }
        logger.info("table:{} field:{} type:{} aggregationType:{} value:{}", table, key, type,
                "percentile", values
        );
        estimationDataMap.put(key, PercentileEstimationData.builder()
                .values(values)
                .count(hits)
                .build());
    } else if(value instanceof Cardinality) {
        Cardinality cardinality = (Cardinality) value;
        logger.info("table:{} field:{} type:{} aggregationType:{} value:{}", table, key, type,
                CARDINALITY, cardinality.getValue()
        );
        EstimationData estimationData = estimationDataMap.get(key.replace("_", ""));
        if (estimationData instanceof PercentileEstimationData) {
            ((PercentileEstimationData) estimationData).setCardinality(cardinality.getValue());
        } else {
            estimationDataMap.put(key.replace("_", ""), PercentileEstimationData.builder()
                    .cardinality(cardinality.getValue())
                    .build());
        }
    }
}
 
Example #7
Source File: SearchServlet.java    From stash-codesearch-plugin with Apache License 2.0 5 votes vote down vote up
private static ImmutableList<ImmutableMap<String, Object>> getSoyPercentileList(
    Percentiles aggregation, double... percentiles) {
    ImmutableList.Builder<ImmutableMap<String, Object>> builder = ImmutableList.builder();
    for (double percentile : percentiles) {
        builder.add(ImmutableMap.<String, Object> of(
            "percentile", percentile,
            "value", aggregation.percentile(percentile)));
    }
    return builder.build();
}
 
Example #8
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 4 votes vote down vote up
private static AggregationResult reduceAggregationResults(ElasticsearchSearchQueryBase query, org.vertexium.query.Aggregation queryAgg, List<Aggregation> resultAggs) {
    if (resultAggs.size() == 0) {
        throw new VertexiumException("Cannot reduce zero sized aggregation list");
    }

    Object aggType = queryAgg;
    if (queryAgg == null) {
        Aggregation first = resultAggs.get(0);
        if (first.getName().endsWith(AGGREGATION_HAS_NOT_SUFFIX)) {
            if (resultAggs.size() > 1) {
                first = resultAggs.get(1);
            } else {
                throw new VertexiumException("Unhandled aggregation. Found HasNot filter with no associated aggregations and no query aggregation.");
            }
        }
        aggType = first;
    }

    if (aggType instanceof HistogramAggregation || aggType instanceof CalendarFieldAggregation ||
        aggType instanceof InternalHistogram || aggType instanceof InternalDateHistogram) {
        return reduceHistogramResults(query, resultAggs);
    }
    if (aggType instanceof RangeAggregation || aggType instanceof InternalRange) {
        return reduceRangeResults(query, resultAggs);
    }
    if (aggType instanceof PercentilesAggregation || aggType instanceof Percentiles) {
        return reducePercentilesResults(query, resultAggs);
    }
    if (aggType instanceof TermsAggregation || aggType instanceof InternalTerms) {
        return reduceTermsResults(query, resultAggs);
    }
    if (aggType instanceof GeohashAggregation || aggType instanceof InternalGeoHashGrid) {
        return reduceGeohashResults(query, resultAggs);
    }
    if (aggType instanceof StatisticsAggregation || aggType instanceof InternalExtendedStats) {
        return reduceStatisticsResults(resultAggs);
    }
    if (aggType instanceof CardinalityAggregation || aggType instanceof InternalCardinality) {
        return reduceCardinalityResults(query, resultAggs);
    }
    throw new VertexiumException("Unhandled aggregation type: " + aggType.getClass().getName());
}