public Long getEventGroupDivceCont(LogRequest req) { String indexName; if (req.getAppId() == null) { indexName = indexNameGenerator.structuredLogPrefixWithoutAppId() + "*"; } else { indexName = indexNameGenerator.structuredLogPrefix() + "*"; } // 创建搜索对象 BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); setQuery(boolQueryBuilder, req); SearchRequest searchRequest = new SearchRequest(indexName); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(boolQueryBuilder); searchSourceBuilder.size(0); AggregationBuilder aggregationBuilder = AggregationBuilders.terms("deviceCode1").field("deviceCode.keyword").size(100000); // TermsAggregationBuilder dataSourceAgg = AggregationBuilders.terms("dataSourceId").field("dataSourceId"); // TermsAggregationBuilder systemIdAgg = AggregationBuilders.terms("systemId").field("systemId"); // dataSourceAgg.subAggregation(systemIdAgg); // aggregationBuilder.subAggregation(dataSourceAgg); searchSourceBuilder.aggregation(aggregationBuilder); searchRequest.source(searchSourceBuilder); try { SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); // 处理响应结果 SearchHits searchHits = searchResponse.getHits(); // 总条数 return searchHits.getTotalHits().value; } catch (IOException e) { e.printStackTrace(); } return 0L; }
标签:indexName,searchSourceBuilder,req,AggregationBuilders,field,dataSourceAgg,分组,jav From: https://www.cnblogs.com/kkvt/p/17803935.html