org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats. 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 StatisticsResult reduceStatisticsResults(List<Aggregation> aggs) {
    List<StatisticsResult> results = new ArrayList<>();
    for (Aggregation agg : aggs) {
        if (agg instanceof ExtendedStats) {
            ExtendedStats extendedStats = (ExtendedStats) agg;
            long count = extendedStats.getCount();
            double sum = extendedStats.getSum();
            double min = extendedStats.getMin();
            double max = extendedStats.getMax();
            double standardDeviation = extendedStats.getStdDeviation();
            results.add(new StatisticsResult(count, sum, min, max, standardDeviation));
        } else {
            throw new VertexiumException("Aggregation is not a statistics: " + agg.getClass().getName());
        }
    }
    return StatisticsResult.combine(results);
}
 
Example #2
Source File: SearchServiceImpl.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
/**
 * 聚合统计,
 * @param hostIp ES集群的ip地址
 * @param clusterName ES集群集群名称
 * @param indexName ES集群的索引名称,可使用多个索引 indexName="test2,test1,test";
 * @param typeName 索引类型,可多个 typeName="doc,pdf,test";
 * @param port ES集群的端口号
 * @param aggFdName 需要统计的字段
 * @param aggType 记录偏移 , null-默认为10
 * @return
 * @throws TException
 */
@Override
public Map<String, String> StatsAggregation(String hostIp, int port, String clusterName, String indexName, String typeName, String aggFdName, String aggType) throws TException {
    Client client=null;
    try {
        client = ESUtils.getClient( hostIp, port, clusterName );
    } catch (Exception e) {
        e.printStackTrace();
    }
    ExtendedStatsAggregationBuilder aggregationBuilder = AggregationBuilders.extendedStats( "agg" ).field( aggFdName );
    SearchResponse response = client.prepareSearch( indexName ).addAggregation( aggregationBuilder ).get();
    ExtendedStats agg = response.getAggregations().get( "agg" );
    Map<String,String> map=new LinkedHashMap<>(  );
    map.put("avg", agg.getAvgAsString() );
    map.put("count", String.valueOf( agg.getCount() ) );
    map.put("sum", agg.getSumAsString() );
    map.put("max", agg.getMaxAsString() );
    map.put("min", agg.getMinAsString() );
    //以字符串形式收集的值的标准偏差。
    map.put( "StdDeviation",agg.getStdDeviationAsString() );
    //平方和
    map.put( "SumOfSquares" ,agg.getSumOfSquaresAsString());
    //方差
    map.put( "Variance",agg.getStdDeviationAsString() );
    System.out.println( "stats avg"+agg.getAvgAsString()+"count"+agg.getCount()+"max"+agg.getMaxAsString()+"min"+agg.getMinAsString()+"sum"+agg.getSumAsString() );

    return map;
}