1、求索引lib3下的age字段的最大值
@Test public void testAggMax() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //創建客戶端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300)); //aggMax是固定寫法,求最大年齡 AggregationBuilder agg = AggregationBuilders.max("aggMax").field("age"); //執行查詢 SearchResponse response = client.prepareSearch("lib3")//索引是lib .addAggregation(agg) .get(); //獲取結果 Max max = response.getAggregations().get("aggMax"); //輸出結果 System.out.println(max.getValue()); client.close(); }
2、求索引lib3下的age字段的最小值
@Test public void testAggMin() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //創建客戶端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300)); //aggMin是固定寫法,求最小年齡 AggregationBuilder agg = AggregationBuilders.min("aggMin").field("age"); //執行查詢 SearchResponse response = client.prepareSearch("lib3")//索引是lib .addAggregation(agg) .get(); //獲取結果 Min min = response.getAggregations().get("aggMin"); //輸出結果 System.out.println(min.getValue()); client.close(); }
3、求索引lib3下的age字段的平均值
@Test public void testAggAvg() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //創建客戶端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300)); //aggAvg是固定寫法,求平均年齡 AggregationBuilder agg = AggregationBuilders.avg("aggAvg").field("age"); //執行查詢 SearchResponse response = client.prepareSearch("lib3")//索引是lib .addAggregation(agg) .get(); //獲取結果 Avg avg = response.getAggregations().get("aggAvg"); //輸出結果 System.out.println(avg.getValue()); client.close(); }
4、求索引lib3下的age字段的和
@Test public void testAggSum() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //創建客戶端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300)); //aggSum是固定寫法,求年齡總和 AggregationBuilder agg = AggregationBuilders.sum("aggSum").field("age"); //執行查詢 SearchResponse response = client.prepareSearch("lib3")//索引是lib .addAggregation(agg) .get(); //獲取結果 Sum sum = response.getAggregations().get("aggSum"); //輸出結果 System.out.println(sum.getValue()); client.close(); }
5、求索引lib3中age字段有多個不同值
@Test public void testAggCardinality() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //創建客戶端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300)); //aggCardinality是固定寫法,求某字段有多個不同值 AggregationBuilder agg = AggregationBuilders.cardinality("aggCardinality").field("age"); //執行查詢 SearchResponse response = client.prepareSearch("lib3")//索引是lib .addAggregation(agg) .get(); //獲取結果 Cardinality cardinality = response.getAggregations().get("aggCardinality"); //輸出結果 System.out.println(cardinality.getValue()); client.close(); }