Java Code Examples for org.elasticsearch.search.aggregations.AggregationBuilder#subAggregation()

The following examples show how to use org.elasticsearch.search.aggregations.AggregationBuilder#subAggregation() . 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: GroupConditionBuilder.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 封装查询请求的聚合条件
 *
 * @param rootTermsBuilder     聚合根
 * @param termsBuilder         当前聚合
 * @param functions            聚合函数集
 * @param searchRequestBuilder 聚合功能查询请求
 */
private void aggregationFunction(AggregationBuilder rootTermsBuilder, AggregationBuilder termsBuilder,
                                 final List<FunctionCondition> functions,
                                 final SearchRequestBuilder searchRequestBuilder) {

    for (FunctionCondition functionCondition : functions) {

        AbstractAggregationBuilder aggregation = getAggregationFunction(functionCondition);

        if (termsBuilder != null) {
            termsBuilder.subAggregation(aggregation);
        } else {
            searchRequestBuilder.addAggregation(aggregation);
        }
    }
    if (termsBuilder != null) {
        searchRequestBuilder.addAggregation(rootTermsBuilder);
    }
}
 
Example 2
Source File: AggregateByQueryParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private AggregationBuilder parseNestedAggregationClauseContext(ElasticsearchParser.NestedAggregationClauseContext nestedAggregationClauseContext) {
    String nestedPath = nestedAggregationClauseContext.nestedPath.getText();
    String nestedName = "nested_" + nestedPath;
    AggregationBuilder aggregationBuilder=null;
    if (nestedAggregationClauseContext.aggregationClause(0).nestedAggregationClause() != null) {
        AggregationBuilder nestedAggregationBuilder = parseNestedAggregationClauseContext(nestedAggregationClauseContext.aggregationClause(0).nestedAggregationClause());
        aggregationBuilder = AggregationBuilders.nested(nestedName, nestedPath).subAggregation(nestedAggregationBuilder);
    } else if (nestedAggregationClauseContext.aggregationClause(0).aggregateItemClause() != null) {
        List<AggregationBuilder> aggregationBuilders = parseAggregateItemClauseContext(nestedAggregationClauseContext.aggregationClause(0).aggregateItemClause());
        AggregationBuilder nestAggregationBuilder = AggregationBuilders.nested(nestedName, nestedPath);
        for (AggregationBuilder agg : aggregationBuilders) {
            nestAggregationBuilder.subAggregation(agg);
        }
        aggregationBuilder = nestAggregationBuilder;
    }
    if(aggregationBuilder!=null){
        if(nestedAggregationClauseContext.subAggregationClause().size()>0){
            parseSubAggregationClauseContext(aggregationBuilder,nestedAggregationClauseContext.subAggregationClause());
        }
        return aggregationBuilder;
    }else {
        throw new ElasticSql2DslException("[syntax error] nested aggregation not support this query format of the sql");
    }
}
 
Example 3
Source File: AggregateByQueryParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private AggregationBuilder parseSubAggregationClauseContext(AggregationBuilder aggregationBuilder, List<ElasticsearchParser.SubAggregationClauseContext> subAggregationClauseContexts) {
    for (ElasticsearchParser.SubAggregationClauseContext subAggregationClauseContext : subAggregationClauseContexts) {
        if (subAggregationClauseContext.aggregationClause().aggregateItemClause() != null) {
            List<AggregationBuilder> aggregationItems = parseAggregateItemClauseContext(subAggregationClauseContext.aggregationClause().aggregateItemClause());
            for (AggregationBuilder aggItem : aggregationItems) {
                aggregationBuilder.subAggregation(aggItem);
            }
            return aggregationBuilder;
        } else if (subAggregationClauseContext.aggregationClause().nestedAggregationClause() != null) {
            AggregationBuilder nestedAggregationBuilder = parseNestedAggregationClauseContext(subAggregationClauseContext.aggregationClause().nestedAggregationClause());
            aggregationBuilder.subAggregation(nestedAggregationBuilder);
            for(int i=1;i<subAggregationClauseContext.aggregationClause().nestedAggregationClause().aggregationClause().size();i++){
                for(AggregationBuilder agg:parseAggregationClauseContext(subAggregationClauseContext.aggregationClause().nestedAggregationClause().aggregationClause(i))){
                    aggregationBuilder.subAggregation(agg);
                }
            }
            return aggregationBuilder;

        }
    }
    throw new ElasticSql2DslException("not support yet");
}
 
Example 4
Source File: SalesTrendsQueryAdapter.java    From micronaut-microservices-poc with Apache License 2.0 6 votes vote down vote up
@Override
SearchRequest buildQuery() {
    SearchRequest searchRequest = new SearchRequest("policy_stats")
            .types("policy_type");

    BoolQueryBuilder filterBuilder = QueryBuilders.boolQuery();
    if (query.getFilterByProductCode()!=null) {
        filterBuilder.must(QueryBuilders.termQuery("productCode.keyword", query.getFilterByProductCode()));
    }
    if (query.getFilterBySalesDate()!=null){
        RangeQueryBuilder datesRange = QueryBuilders
                .rangeQuery("from")
                .gte(query.getFilterBySalesDate().getFrom().toString())
                .lt(query.getFilterBySalesDate().getTo().toString());
        filterBuilder.must(datesRange);
    }
    AggregationBuilder aggBuilder = AggregationBuilders.filter("agg_filter",filterBuilder);

    DateHistogramAggregationBuilder histBuilder = AggregationBuilders
            .dateHistogram("sales")
            .field("from")
            .dateHistogramInterval(query.getAggregationUnit().toDateHistogramInterval())
            .subAggregation(AggregationBuilders.sum("total_premium").field("totalPremium"));
    aggBuilder.subAggregation(histBuilder);

    SearchSourceBuilder srcBuilder = new SearchSourceBuilder()
            .aggregation(aggBuilder)
            .size(0);
    searchRequest.source(srcBuilder);

    return searchRequest;
}
 
Example 5
Source File: GroupConditionBuilder.java    From search-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
public SearchRequestBuilder build(final SearchRequestBuilder searchRequestBuilder, final QueryESObject esObject) {

        final GroupByCondition groupByCondition = esObject.getGroupByCondition();
        if (groupByCondition == null) {
            return searchRequestBuilder;
        }
        final List<String> groupFields = groupByCondition.getGroupFields();
        if (groupFields == null) {
            return searchRequestBuilder;
        }
        // 根聚合嵌套使用
        AggregationBuilder rootTermsBuilder = null;
        // 最底层嵌套聚合
        AggregationBuilder termTermsBuilder = null;
        for (int i = 0; i < groupFields.size(); i++) {
            if (rootTermsBuilder == null) {
                rootTermsBuilder = AggregationBuilders.terms(groupFields.get(i))
                        .field(groupFields.get(i));
            } else {
                if (null == termTermsBuilder || termTermsBuilder == rootTermsBuilder) {
                    termTermsBuilder = AggregationBuilders.terms(groupFields.get(i)).field(groupFields.get(i));
                    rootTermsBuilder.subAggregation(termTermsBuilder);
                } else {
                    final AggregationBuilder currentTermsBuilder = AggregationBuilders.terms(groupFields.get(i))
                            .field(groupFields.get(i))
                            .minDocCount(0);
                    termTermsBuilder.subAggregation(currentTermsBuilder);
                    termTermsBuilder = currentTermsBuilder;
                }
            }
        }

        aggregationFunction(rootTermsBuilder, termTermsBuilder, groupByCondition.getFunctionConditions(),
                searchRequestBuilder);

        return searchRequestBuilder;
    }
 
Example 6
Source File: TotalSalesQueryAdapter.java    From micronaut-microservices-poc with Apache License 2.0 5 votes vote down vote up
@Override
SearchRequest buildQuery() {
    SearchRequest searchRequest = new SearchRequest("policy_stats")
            .types("policy_type");

    BoolQueryBuilder filterBuilder = QueryBuilders.boolQuery();
    if (query.getFilterByProductCode()!=null) {
        filterBuilder.must(QueryBuilders.termQuery("productCode.keyword", query.getFilterByProductCode()));
    }
    if (query.getFilterBySalesDate()!=null){
        RangeQueryBuilder datesRange = QueryBuilders
                .rangeQuery("from")
                .gte(query.getFilterBySalesDate().getFrom().toString())
                .lt(query.getFilterBySalesDate().getTo().toString());
        filterBuilder.must(datesRange);
    }
    AggregationBuilder aggBuilder = AggregationBuilders.filter("agg_filter",filterBuilder);

    TermsAggregationBuilder sumAggBuilder = AggregationBuilders
            .terms("count_by_product")
            .field("productCode.keyword")
            .subAggregation(AggregationBuilders.sum("total_premium").field("totalPremium"));
    aggBuilder.subAggregation(sumAggBuilder);

    SearchSourceBuilder srcBuilder = new SearchSourceBuilder()
            .aggregation(aggBuilder)
            .size(0);
    searchRequest.source(srcBuilder);

    return searchRequest;
}
 
Example 7
Source File: AgentSalesQueryAdapter.java    From micronaut-microservices-poc with Apache License 2.0 5 votes vote down vote up
@Override
SearchRequest buildQuery() {
    SearchRequest searchRequest = new SearchRequest("policy_stats")
            .types("policy_type");

    BoolQueryBuilder filterBuilder = QueryBuilders.boolQuery();
    if (query.getFilterByAgentLogin() != null) {
        filterBuilder.must(QueryBuilders.termQuery("agentLogin.keyword", query.getFilterByAgentLogin()));
    }
    if (query.getFilterByProductCode() != null) {
        filterBuilder.must(QueryBuilders.termQuery("productCode.keyword", query.getFilterByProductCode()));
    }
    if (query.getFilterBySalesDate() != null) {
        RangeQueryBuilder datesRange = QueryBuilders
                .rangeQuery("from")
                .gte(query.getFilterBySalesDate().getFrom().toString())
                .lt(query.getFilterBySalesDate().getTo().toString());
        filterBuilder.must(datesRange);
    }
    AggregationBuilder aggBuilder = AggregationBuilders.filter("agg_filter", filterBuilder);

    TermsAggregationBuilder sumAggBuilder = AggregationBuilders
            .terms("count_by_agent")
            .field("agentLogin.keyword")
            .subAggregation(AggregationBuilders.sum("total_premium").field("totalPremium"));
    aggBuilder.subAggregation(sumAggBuilder);

    SearchSourceBuilder srcBuilder = new SearchSourceBuilder()
            .aggregation(aggBuilder)
            .size(0);
    searchRequest.source(srcBuilder);

    return searchRequest;
}
 
Example 8
Source File: ElasticsearchHelperService.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 按天统计  各个日志级别的数量 查询条件
 *
 * @param EsModel es
 *                keywords
 *                highlights
 * @return
 */
public Search statisticsDayCondition(EsModel es) {
    Search search = null;
    if (es == null) {
        return search;
    }
    String indexName = es.getIndex();
    String type = es.getType();
    String startTime = es.getStartTime();
    String endTime = es.getEndTime();
    JSONObject keywords = es.getKeyWord();
    if (StringUtil.isBlank(indexName)) {
        return search;
    }
    if (StringUtil.isBlank(type)) {
        return search;
    }
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    if (StringUtil.isNotBlank(startTime) && StringUtil.isNotBlank(endTime)) {
        RangeQueryBuilder rangequerybuilder = QueryBuilders
                .rangeQuery("createTime")
                .from(startTime).to(endTime);
        searchSourceBuilder.query(rangequerybuilder);
    }
    AddKeyWords(keywords, searchSourceBuilder);
    ExtendedBounds extendedBounds = new ExtendedBounds(startTime, endTime);
    AggregationBuilder levelAgg = AggregationBuilders.terms("level_count").field("level").minDocCount(0);
    AggregationBuilder dateAgg = AggregationBuilders.dateHistogram("day_count")
            .field("createTime")
            .dateHistogramInterval(DateHistogramInterval.DAY)
            .format("yyyy-MM-dd")
            .extendedBounds(extendedBounds)
            .minDocCount(0L)//为空0补充
            .timeZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+8")));
    AggregationBuilder builder = dateAgg.subAggregation(levelAgg);
    searchSourceBuilder.aggregation(builder).size(0);
    search = new Search.Builder(searchSourceBuilder.toString())
            .addIndex(indexName).addType(type).build();
    return search;
}
 
Example 9
Source File: EsAggregationSearchTest.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
 * @Author pancm
 * @Description 多个聚合条件测试
 * SQL: select age, name, count(*) as count1 from student group by age, name;
 * @Date  2019/7/3
 * @Param []
 * @return void
 **/
private static void groupbySearch() throws IOException{
    String buk="group";
    AggregationBuilder aggregation = AggregationBuilders.terms("age").field("age");
    AggregationBuilder aggregation2 = AggregationBuilders.terms("name").field("name");
    //根据创建时间按天分组
    AggregationBuilder aggregation3 = AggregationBuilders.dateHistogram("createtm")
            .field("createtm")
            .format("yyyy-MM-dd")
            .dateHistogramInterval(DateHistogramInterval.DAY);

    aggregation2.subAggregation(aggregation3);
    aggregation.subAggregation(aggregation2);
    agg(aggregation,buk);
}
 
Example 10
Source File: GroupParser.java    From sql4es with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a set of 'leaf aggregations' to the provided parent metric (i.e. count, sum, max etc)
 * @param parentAgg
 * @param heading
 * @param addCount
 */
@SuppressWarnings("rawtypes")
private void addMetrics(AggregationBuilder parentAgg, Heading heading, boolean addCount){
	for(Column metric : heading.columns()){
		AggregationBuilder agg = null;
	    if(metric.getOp() == Operation.AVG) {
               agg = AggregationBuilders.avg(metric.getAggName());
           } else if(addCount && metric.getOp() == Operation.COUNT) {
               agg = AggregationBuilders.count(metric.getAggName());
           } else if(metric.getOp() == Operation.MAX) {
               agg = AggregationBuilders.max(metric.getAggName());
           } else if(metric.getOp() == Operation.MIN) {
               agg = AggregationBuilders.min(metric.getAggName());
           } else if(metric.getOp() == Operation.SUM) {
               agg = AggregationBuilders.sum(metric.getAggName());
           }
		if (agg != null) {
	        String col = metric.getColumn();
	        // * or number
	        if ("*".equals(col) || col.matches("-?\\d+")) {
                   agg = ((ValuesSourceAggregationBuilder.LeafOnly)agg).script(new Script("1"));
               } else if (col.startsWith("script:")) {
                   agg = ((ValuesSourceAggregationBuilder.LeafOnly)agg).script(new Script(col.replaceAll("script:","")));
               } else {
                   agg = ((ValuesSourceAggregationBuilder.LeafOnly) agg).field(metric.getColumn());
               }
               parentAgg.subAggregation(agg);
           }
	}
}
 
Example 11
Source File: AggregationQueryAction.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private boolean insertFilterIfExistsAfter(AggregationBuilder agg, List<Field> groupBy, AggregationBuilder builder, int nextPosition) throws SqlParseException {
    if (groupBy.size() <= nextPosition) return false;
    Field filterFieldCandidate = groupBy.get(nextPosition);
    if (!(filterFieldCandidate instanceof MethodField)) return false;
    MethodField methodField = (MethodField) filterFieldCandidate;
    if (!methodField.getName().toLowerCase().equals("filter")) return false;
    builder.subAggregation(aggMaker.makeGroupAgg(filterFieldCandidate, select).subAggregation(agg));
    return true;
}
 
Example 12
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 13
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 14
Source File: AggMaker.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private AggregationBuilder addFieldToAgg(MethodField field, ValuesSourceAggregationBuilder builder) {
    KVValue kvValue = field.getParams().get(0);
    if (kvValue.key != null && kvValue.key.equals("script")) {
        if (kvValue.value instanceof MethodField) {
            return builder.script(new Script(((MethodField) kvValue.value).getParams().get(1).toString()));
        } else {
            return builder.script(new Script(kvValue.value.toString()));
        }

    } else if (kvValue.key != null && kvValue.value.toString().trim().startsWith("def")) {
        return builder.script(new Script(kvValue.value.toString()));
    } else if (kvValue.key != null && (kvValue.key.equals("nested") || kvValue.key.equals("reverse_nested"))) {
        NestedType nestedType = (NestedType) kvValue.value;

        builder.field(nestedType.field);

        AggregationBuilder nestedBuilder;

        String nestedAggName = nestedType.field + "@NESTED";

        if (nestedType.isReverse()) {
            if (nestedType.path != null && nestedType.path.startsWith("~")) {
                String realPath = nestedType.path.substring(1);
                nestedBuilder = AggregationBuilders.nested(nestedAggName,realPath);
                nestedBuilder = nestedBuilder.subAggregation(builder);
                return AggregationBuilders.reverseNested(nestedAggName + "_REVERSED").subAggregation(nestedBuilder);
            } else {
                ReverseNestedAggregationBuilder reverseNestedAggregationBuilder = AggregationBuilders.reverseNested(nestedAggName);
                if (nestedType.path!=null){
                    reverseNestedAggregationBuilder.path(nestedType.path);
                }
                nestedBuilder = reverseNestedAggregationBuilder;
            }
        } else {
            nestedBuilder = AggregationBuilders.nested(nestedAggName,nestedType.path);
        }

        return nestedBuilder.subAggregation(builder);
    } else if (kvValue.key != null && (kvValue.key.equals("children"))) {
        ChildrenType childrenType = (ChildrenType) kvValue.value;

        builder.field(childrenType.field);

        AggregationBuilder childrenBuilder;

        String childrenAggName = childrenType.field + "@CHILDREN";

        childrenBuilder = JoinAggregationBuilders.children(childrenAggName,childrenType.childType);

        return childrenBuilder;
    }

    return builder.field(kvValue.toString());
}
 
Example 15
Source File: AggregationQueryAction.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private void explanFields(SearchRequestBuilder request, List<Field> fields, AggregationBuilder groupByAgg) throws SqlParseException {
    for (Field field : fields) {

        if (field instanceof MethodField) {

            if (field.getName().equals("script")) {
                //question addStoredField()是什么鬼?
                request.addStoredField(field.getAlias());

                /*
                zhongshu-comment 将request传进去defaultQueryAction对象是为了调用setFields()中的这一行代码:request.setFetchSource(),
                                 给request设置include字段和exclude字段
                 */
                DefaultQueryAction defaultQueryAction = new DefaultQueryAction(client, select);
                defaultQueryAction.intialize(request);
                List<Field> tempFields = Lists.newArrayList(field);
                defaultQueryAction.setFields(tempFields);

                /*
                 zhongshu-comment 因为field.getName().equals("script")的那些字段一般都是作为维度而不是统计指标、度量metric,
                                    所以就要continue,不能继续下边的创建agg
                */
                continue;
            }

            //modify by xzb 类型无法转换,只能新增一个 makeMovingFieldAgg方法
            if (groupByAgg != null) {
                if (field.getName().startsWith("rollingstd") || field.getName().startsWith("movingavg")) {
                    groupByAgg.subAggregation(aggMaker.makeMovingFieldAgg((MethodField) field, groupByAgg));
                } else {
                    groupByAgg.subAggregation(aggMaker.makeFieldAgg((MethodField) field, groupByAgg));
                }
            } else {
                //question 不懂为什么将一个null的agg加到request中,这应该是dsl语法问题,先不需要深究
                request.addAggregation(aggMaker.makeFieldAgg((MethodField) field, groupByAgg));
            }
        } else if (field instanceof Field) {

            //question 为什么Filed类型的字段不需要像MethodField类型字段一样设置include、exclude字段:request.setFetchSource()
            request.addStoredField(field.getName());
        } else {
            throw new SqlParseException("it did not support this field method " + field);
        }
    }
}