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

The following examples show how to use org.elasticsearch.search.aggregations.metrics.percentiles.PercentilesAggregationBuilder. 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: PercentilesAggregationMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
        RestHighLevelClient client = HighLevelClient.getInstance();
        try{
            QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "weather_hourforcast");
            PercentilesAggregationBuilder aggregationBuilder = AggregationBuilders.percentiles("utm").field("utm").keyed(false);
//            PercentilesAggregationBuilder aggregationBuilder = AggregationBuilders.percentiles("utm").field("utm").percentiles(95,99,99.9).keyed(false);//自定义百分区间

            SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index
            searchRequest.types("log");//限定type

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(matchQueryBuilder);
            searchSourceBuilder.aggregation(aggregationBuilder);
            searchSourceBuilder.size(0);
            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResponse = client.search(searchRequest);
            System.out.println(searchResponse);

        }finally{
            HighLevelClient.close();
        }
    }
 
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: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 5 votes vote down vote up
protected List<AbstractAggregationBuilder> getElasticsearchPercentilesAggregations(PercentilesAggregation agg) {
    String propertyName = getSearchIndex().addVisibilityToPropertyName(getGraph(), agg.getFieldName(), agg.getVisibility());
    String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyName);
    String aggName = createAggregationName(agg.getAggregationName(), visibilityHash);
    PercentilesAggregationBuilder percentilesAgg = AggregationBuilders.percentiles(aggName);
    percentilesAgg.field(propertyName);
    if (agg.getPercents() != null && agg.getPercents().length > 0) {
        percentilesAgg.percentiles(agg.getPercents());
    }
    return Collections.singletonList(percentilesAgg);
}