Java Code Examples for org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder#subAggregation()

The following examples show how to use org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder#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: ElasticsearchSearchDao.java    From metron with Apache License 2.0 6 votes vote down vote up
private TermsAggregationBuilder getGroupsTermBuilder(GroupRequest groupRequest, int index) {
  List<Group> groups = groupRequest.getGroups();
  Group group = groups.get(index);
  String aggregationName = getGroupByAggregationName(group.getField());
  TermsAggregationBuilder termsBuilder = AggregationBuilders.terms(aggregationName);
  termsBuilder
      .field(group.getField())
      .size(accessConfig.getMaxSearchGroups())
      .order(getElasticsearchGroupOrder(group.getOrder()));
  if (index < groups.size() - 1) {
    termsBuilder.subAggregation(getGroupsTermBuilder(groupRequest, index + 1));
  }
  Optional<String> scoreField = groupRequest.getScoreField();
  if (scoreField.isPresent()) {
    SumAggregationBuilder scoreSumAggregationBuilder = AggregationBuilders.sum(getSumAggregationName(scoreField.get())).field(scoreField.get()).missing(0);
    termsBuilder.subAggregation(scoreSumAggregationBuilder);
  }
  return termsBuilder;
}
 
Example 2
Source File: HistogramAggregationMain.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{
            HistogramAggregationBuilder histogramAggregationBuilder = AggregationBuilders.histogram("utm_histogram");
            histogramAggregationBuilder.field("utm");//设置直方图针对的字段
            histogramAggregationBuilder.interval(1000);//直方图每个分组对应的范围
            histogramAggregationBuilder.order(BucketOrder.aggregation("_key",true));//分组key的排序
//            histogramAggregationBuilder.minDocCount(0);//对于每个分组最少具有多少条数据,少于这个设置,则该分组不显示
//            histogramAggregationBuilder.extendedBounds(0,8000);//设置分组区间的下线和上线,只有当min_doc_count为0时有效

            TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("top_cmd");
            aggregationBuilder.field("cmd");
            aggregationBuilder.size(3);
            aggregationBuilder.subAggregation(histogramAggregationBuilder);

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

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.aggregation(aggregationBuilder);
            searchRequest.source(searchSourceBuilder);

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

        }finally{
            HighLevelClient.close();
        }
    }
 
Example 3
Source File: EsAggregationSearchTest.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
 * @Author pancm
 * @Description 平均聚合查询测试用例
 * @Date  2019/4/1
 * @Param []
 * @return void
 **/
private static  void avgGroupSearch() throws IOException {


    String agg="t_class_avg";
    String buk="t_grade";
    //terms 就是分组统计 根据student的grade成绩进行分组并创建一个新的聚合
    TermsAggregationBuilder aggregation = AggregationBuilders.terms(agg).field("class");
    aggregation.subAggregation(AggregationBuilders.avg(buk).field("grade"));

    logger.info("根据班级求平均分数:");
    agg(aggregation,agg,buk);

}
 
Example 4
Source File: EsAggregationSearchTest.java    From java-study with Apache License 2.0 5 votes vote down vote up
private static  void maxGroupSearch() throws  IOException{

        String agg="t_class_max";
        String buk="t_grade";
        //terms 就是分组统计 根据student的grade成绩进行分组并创建一个新的聚合
        TermsAggregationBuilder aggregation = AggregationBuilders.terms(agg).field("class");
        aggregation.subAggregation(AggregationBuilders.max(buk).field("grade"));
        logger.info("根据班级求最大分数:");
        agg(aggregation,agg,buk);
    }
 
Example 5
Source File: EsAggregationSearchTest.java    From java-study with Apache License 2.0 5 votes vote down vote up
private static  void sumGroupSearch() throws  IOException{
    String agg="t_class_sum";
    String buk="t_grade";
    //terms 就是分组统计 根据student的grade成绩进行分组并创建一个新的聚合
    TermsAggregationBuilder aggregation = AggregationBuilders.terms(agg).field("class");
    aggregation.subAggregation(AggregationBuilders.sum(buk).field("grade"));

    logger.info("根据班级求总分:");
    agg(aggregation,agg,buk);
}
 
Example 6
Source File: GroupParser.java    From sql4es with Apache License 2.0 5 votes vote down vote up
/**
 * Adds aggregations recursively
 * All metric columns are added to last aggregation
 * @param aggs
 * @param index
 * @param state
 * @return
 */
private TermsAggregationBuilder buildAggregationQuery(List<Column> aggs, int index, QueryState state){
	Column agg = aggs.get(index);
	TermsAggregationBuilder result = null;
	if(aggs.get(index).getOp() == Operation.NONE){
		result = AggregationBuilders.terms(agg.getAggName()).field(agg.getColumn());
		result.size(state.getIntProp(Utils.PROP_FETCH_SIZE, 10000));
	}
	if(index < aggs.size() - 1) result.subAggregation(buildAggregationQuery(aggs, index+1, state));
	else addMetrics(result, state.getHeading(), true); 
	return result;
}
 
Example 7
Source File: MetricsQueryEs7DAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
protected void functionAggregation(Function function, TermsAggregationBuilder parentAggBuilder, String valueCName) {
    switch (function) {
        case Avg:
            parentAggBuilder.subAggregation(AggregationBuilders.avg(valueCName).field(valueCName));
            break;
        case Sum:
            parentAggBuilder.subAggregation(AggregationBuilders.sum(valueCName).field(valueCName));
            break;
        default:
            parentAggBuilder.subAggregation(AggregationBuilders.avg(valueCName).field(valueCName));
            break;
    }
}
 
Example 8
Source File: MetricsQueryEsDAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
protected void functionAggregation(Function function, TermsAggregationBuilder parentAggBuilder, String valueCName) {
    switch (function) {
        case Avg:
            parentAggBuilder.subAggregation(AggregationBuilders.avg(valueCName).field(valueCName));
            break;
        case Sum:
            parentAggBuilder.subAggregation(AggregationBuilders.sum(valueCName).field(valueCName));
            break;
        default:
            parentAggBuilder.subAggregation(AggregationBuilders.avg(valueCName).field(valueCName));
            break;
    }
}
 
Example 9
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 10
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);
    }