org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder. 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: MaxMinAggregationMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
        RestHighLevelClient client = HighLevelClient.getInstance();
        try{
//            MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("utm").field("utm");
            MinAggregationBuilder aggregationBuilder = AggregationBuilders.min("utm").field("utm");

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

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.aggregation(aggregationBuilder);
            searchSourceBuilder.size(0);//设置不需要文档数据,只需要返回聚合结果
            searchRequest.source(searchSourceBuilder);

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

            //统计结果
            Aggregations aggregations = searchResponse.getAggregations();
            Map<String, Aggregation> aggregationMap = aggregations.asMap();
            for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){
//                System.out.println(((ParsedMax)(each.getValue())).getValue());
                System.out.println(((ParsedMin)(each.getValue())).getValue());
            }

        }finally{
            HighLevelClient.close();
        }
    }
 
Example #2
Source File: ElasticsearchAggregate.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public void implement(Implementor implementor) {
        implementor.visitChild(0, getInput());
        ElasticsearchTable esTable = implementor.getElasticsearchTable();
        List<AggregateCall> aggCallList = getAggCallList();
        List<RelDataTypeField> fieldList = esTable.getRowType().getFieldList();
        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        final List<String> outNames = ElasticsearchRules.elasticsearchFieldNames(getRowType());
        esTable.setOutNames(outNames);

        //        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        TermsAggregationBuilder groupAggregationBuilder = null;
        AggregationBuilder groupAggregationBuilder2 = null;     //防止出现group但没有sort出错的情况
        for(int i = 0 ;i<groupSet.cardinality();i++) {
            final String inName = inNames.get(groupSet.nth(i));

            groupAggregationBuilder = AggregationBuilders.terms(inName.toLowerCase()).field(transFieldName(ElasticsearchRules.maybeQuote(inName).toLowerCase(),fieldList));
            groupAggregationBuilder2 = AggregationBuilders.terms(inName).field(transFieldName(ElasticsearchRules.maybeQuote(inName),fieldList));
            esTable.setIsGroup(true);
        }
        List<String> out = new ArrayList<String>();   //聚合函数的名称
        for(AggregateCall call : aggCallList)
        {
            SqlAggFunction function = call.getAggregation();
            List<Integer> argList = call.getArgList();
            String functionName = function.getName();
            out.add(functionName);
            switch (function.getKind())
            {
                case MIN:
                    RelDataTypeField typeField = fieldList.get(argList.get(0));
                    if (groupAggregationBuilder == null) {
                        //min值 与 原字段值 的类型是一样的
                        esTable.addAggregationBuilder(AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField.getType()).getJavaClass());
                    }else {
                        MinAggregationBuilder minAgg = AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase());
//                        esTable.addAggregationBuilder(groupAggregationBuilder.subAggregation(minAgg), call.getType().getClass());
                        groupAggregationBuilder.subAggregation(minAgg);
                        groupAggregationBuilder2.subAggregation(minAgg);
                    }
                    break;
                case MAX:
                    //max值 与 原字段值 的类型是一样的
                    RelDataTypeField typeField1 = fieldList.get(argList.get(0));
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField1.getType()).getJavaClass());
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()));
                    }
                    break;
                case COUNT:
                    if(groupAggregationBuilder == null) {
                        if (argList == null || argList.size() == 0)//count(*)
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName), Long.class);
                        else
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)), Long.class);
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                        groupAggregationBuilder2.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                    }
                    break;
                case SUM:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        String s = fieldList.get(argList.get(0)).getName().toLowerCase();
                        SumAggregationBuilder sumAgg = AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(sumAgg);
                        groupAggregationBuilder2.subAggregation(sumAgg);
                    }
                    break;
                case AVG:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        AvgAggregationBuilder avgAgg = AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(avgAgg);
                        groupAggregationBuilder2.subAggregation(avgAgg);
                    }
                    break;
                default:break;
            }
        }
        if (groupAggregationBuilder != null) {
            esTable.addAggregationBuilder((AbstractAggregationBuilder) groupAggregationBuilder,String.class);
            esTable.addAggregationBuilderList2(groupAggregationBuilder);
        }
        esTable.setOut(out);
    }
 
Example #3
Source File: ElasticsearchAggregate.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public void implement(Implementor implementor) {
        implementor.visitChild(0, getInput());
        ElasticsearchTable esTable = implementor.getElasticsearchTable();
        List<AggregateCall> aggCallList = getAggCallList();
        List<RelDataTypeField> fieldList = esTable.getRowType().getFieldList();
        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        final List<String> outNames = ElasticsearchRules.elasticsearchFieldNames(getRowType());
        esTable.setOutNames(outNames);

        //        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        TermsAggregationBuilder groupAggregationBuilder = null;
        AggregationBuilder groupAggregationBuilder2 = null;     //防止出现group但没有sort出错的情况
        for(int i = 0 ;i<groupSet.cardinality();i++) {
            final String inName = inNames.get(groupSet.nth(i));

            groupAggregationBuilder = AggregationBuilders.terms(inName.toLowerCase()).field(transFieldName(ElasticsearchRules.maybeQuote(inName).toLowerCase(),fieldList));
            groupAggregationBuilder2 = AggregationBuilders.terms(inName).field(transFieldName(ElasticsearchRules.maybeQuote(inName),fieldList));
            esTable.setIsGroup(true);
        }
        List<String> out = new ArrayList<String>();   //聚合函数的名称
        for(AggregateCall call : aggCallList)
        {
            SqlAggFunction function = call.getAggregation();
            List<Integer> argList = call.getArgList();
            String functionName = function.getName();
            out.add(functionName);
            switch (function.getKind())
            {
                case MIN:
                    RelDataTypeField typeField = fieldList.get(argList.get(0));
                    if (groupAggregationBuilder == null) {
                        //min值 与 原字段值 的类型是一样的
                        esTable.addAggregationBuilder(AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField.getType()).getJavaClass());
                    }else {
                        MinAggregationBuilder minAgg = AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase());
//                        esTable.addAggregationBuilder(groupAggregationBuilder.subAggregation(minAgg), call.getType().getClass());
                        groupAggregationBuilder.subAggregation(minAgg);
                        groupAggregationBuilder2.subAggregation(minAgg);
                    }
                    break;
                case MAX:
                    //max值 与 原字段值 的类型是一样的
                    RelDataTypeField typeField1 = fieldList.get(argList.get(0));
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField1.getType()).getJavaClass());
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()));
                    }
                    break;
                case COUNT:
                    if(groupAggregationBuilder == null) {
                        if (argList == null || argList.size() == 0)//count(*)
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName), Long.class);
                        else
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)), Long.class);
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                        groupAggregationBuilder2.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                    }
                    break;
                case SUM:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        String s = fieldList.get(argList.get(0)).getName().toLowerCase();
                        SumAggregationBuilder sumAgg = AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(sumAgg);
                        groupAggregationBuilder2.subAggregation(sumAgg);
                    }
                    break;
                case AVG:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        AvgAggregationBuilder avgAgg = AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(avgAgg);
                        groupAggregationBuilder2.subAggregation(avgAgg);
                    }
                    break;
                default:break;
            }
        }
        if (groupAggregationBuilder != null) {
            esTable.addAggregationBuilder((AbstractAggregationBuilder) groupAggregationBuilder,String.class);
            esTable.addAggregationBuilderList2(groupAggregationBuilder);
        }
        esTable.setOut(out);
    }