(22)ElasticSearch java项目中聚合运算求最大、最小、平均、求和以及求不同值
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(); }