赞
踩
上代码
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());
es7.xx
完事!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。