• java Elasticsearch 进行嵌套子聚合


    聚合子查询:

    TermsAggregationBuilder aggregation = AggregationBuilders.terms("dt_id").field("dt_ids").size(30000);
            String agg_area_field = "city_code";
            TermsAggregationBuilder area_field_builder = AggregationBuilders.terms("area_field").field(agg_area_field).size(30000);
            area_field_builder.subAggregation(aggregation);
            SearchResponse response = client.prepareSearch(index_name).setTypes("lw_devices")
                    .setQuery(boolQuery)
                    .addAggregation(area_field_builder)
                    .execute()
                    .actionGet();

    以上demo的大致的意思就是,先对city_code字段进行聚合,然后对聚合结果,再用dt_ids字段进行嵌套聚合(子聚合) , 相当于sql的两个group by , 一个聚合嵌套于另一个聚合之内,可以用subAggregation方法进行关联。

    然后就是对聚合结果的遍历:

            Terms terms = response.getAggregations().get("area_field");
            List<Terms.Bucket> buckets = terms.getBuckets();for (Terms.Bucket bucket : buckets) {
                //地区
                String code = (String) bucket.getKey();
                System.out.println(code);
                Aggregations aggregations = bucket.getAggregations();
                Terms dt_id = aggregations.get("dt_id");
                List<Terms.Bucket> buckets1 = dt_id.getBuckets();
                for (Terms.Bucket bucket1 : buckets1) {
                    System.out.println(bucket1.getKey() + ":" + bucket1.getDocCount());
                }
            }

    思路就是,先根据response拿到aggregation,然后根据标识符拿到对应的,聚合类型,然后得到bucket的集合。然后再通过bucket拿到aggregation, 然后以此循环往下提取聚合结果。

  • 相关阅读:
    自我理解:const char*, char const* and char *const
    获取任意控件的ID或窗口句柄
    Webmax 2.0开启无限量下载
    dotLucene搜索加入中文分词
    SQL SERVER获得指定表的主键
    LogExplore应用(ms sql server事物分析、数据恢复工具)[转载]
    电脑病之一
    查找数据库中记录数大于N的表
    视图
    获取生日提醒数据
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/11583915.html
Copyright © 2020-2023  润新知