当前位置:   article > 正文

java中es多字段分组查询_java es 分组

java es 分组

上代码

            searchRequest.indices("index");//指定要查询的索引
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("quereTime"); //date字段
            rangeQueryBuilder.from(esStartTime).to(esEndTime);//范围查询
             boolQueryBuilder.must(rangeQueryBuilder);
            TermQueryBuilder tb2 = QueryBuilders.termQuery("sysType", "2");
            boolQueryBuilder.must(tb2);//条件查询
            searchSourceBuilder.query(boolQueryBuilder);
            searchSourceBuilder.size(0);
             //聚合分組查詢
            TermsAggregationBuilder ak = AggregationBuilders.terms("ak").field("**Key");
            TermsAggregationBuilder ui = AggregationBuilders.terms("ui").field("**Id");
            TermsAggregationBuilder pi = AggregationBuilders.terms("pi").field("**Id");
            ak.subAggregation(ui.subAggregation(pi));//mysql中的group ak,ui,pi
            searchSourceBuilder.aggregation(ak);
            searchSourceBuilder.trackTotalHits(true);
            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            List<info> ecList = new ArrayList<>();
            Terms ak1 = (Terms) searchResponse.getAggregations().asMap().get("ak");
            for (Terms.Bucket bucket : ak1.getBuckets()) {//分组有几个字段,你就套几个循环
                Terms ui1 = (Terms) bucket.getAggregations().asMap().get("ui");
                for (Terms.Bucket bucket1 : ui1.getBuckets()) {
                    Terms pi1 = (Terms) bucket1.getAggregations().asMap().get("pi");
                    for (Terms.Bucket bucket2 : pi1.getBuckets()) {
                        info a = new info ();
                        a.set**Key(bucket.getKey().toString());
                        a.set**Id(Integer.valueOf(bucket1.getKey().toString()));
                        a.set**Id(Integer.valueOf(bucket2.getKey().toString()));
                        ecList.add(a);

                    }
                }
       }
 System.out.println(info.toString()); 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

es7.xx
完事!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/68143
推荐阅读
相关标签
  

闽ICP备14008679号