public class ESTest_Doc_Query { ? ? public static void main(String[] args) throws Exception { ? ? ? ? //创建ES客户端 ? ? ? ? RestHighLevelClient esClient = new RestHighLevelClient( ? ? ? ? ? ? ? ? RestClient.builder(new HttpHost("localhost",9200,"http")) ? ? ? ? );
? ? ? ? //11、分组查询 ? ? ? ? SearchRequest request = new SearchRequest(); ? ? ? ? request.indices("user");
? ? ? ? SearchSourceBuilder builder = new SearchSourceBuilder(); ? ? ? ? AggregationBuilder ?aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
? ? ? ? builder.aggregation(aggregationBuilder); ? ? ? ? request.source(builder); ? ? ? ? SearchResponse response = esClient.search(request, RequestOptions.DEFAULT); ? ? ? ? SearchHits hits = response.getHits(); ? ? ? ? System.out.println(hits.getTotalHits()); ? ? ? ? System.out.println(response.getTook());
? ? ? ? for(SearchHit hit : hits){ ? ? ? ? ? ? System.out.println(hit.getSourceAsString()); ? ? ? ? } ? ? ? ?? ? ? ? ? //关闭ES客户端 ? ? ? ? esClient.close(); ? ? } }
|