java操作elasticsearch实现聚合查询

1、max 最大值

//max 求最大值
@Test
public void test30() throws UnknownHostException{
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
//aggMax为最大值的别名 ,age是要求最大值的列
AggregationBuilder builder = AggregationBuilders.max("aggMax").field("age");
SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).get();
Max max = response.getAggregations().get("aggMax");
//打印最大值
System.out.println(max.getValue());
}

2、min:最小值

//min 求最小值
@Test
public void test31() throws UnknownHostException{
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
//aggMin为最小值的别名 ,age是要求最小值的列
AggregationBuilder builder = AggregationBuilders.min("aggMin").field("age");
SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).get();
Min min = response.getAggregations().get("aggMin");
//打印最小值
System.out.println(min.getValue());
}

3、avg:平均值

//avg 求平均值
@Test
public void test32() throws UnknownHostException{
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
//aggavg为平均值的别名 ,age是要求最大值的列
AggregationBuilder builder = AggregationBuilders.avg("aggavg").field("age");
SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).get();
Avg avg = response.getAggregations().get("aggavg");
//打印平均值
System.out.println(avg.getValue());
}

4、sum:求和

//sum 求和
@Test
public void test33() throws UnknownHostException{
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
//aggSum为总和的别名 ,age是要求总和的列
AggregationBuilder builder = AggregationBuilders.max("aggSum").field("age");
SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).get();
Sum sum = response.getAggregations().get("aggSum");
//打印总和
System.out.println(sum.getValue());
}

5、cardinality:求基数

  说明:基数就是互不相同的个数,具体看代码及注释

//cardinality基数(互不相同的个数)
@Test
public void test34() throws UnknownHostException{
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
//Cardinality基数查询: 查询age字段中互不相同的个数
AggregationBuilder builder = AggregationBuilders.cardinality("aggCardinality").field("age");
SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).get();
Cardinality cardinality = response.getAggregations().get("aggCardinality");
//打印基数
System.out.println(cardinality.getValue());
}

下一篇博客本人将书写java操作elasticsearch实现query String。对后期博客感兴趣的朋友可以关注交流,转发请说明出处,本人的博客地址为:https://www.cnblogs.com/chenyuanbo/

技术在于交流!

上一篇:java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询


下一篇:java操作elasticsearch实现查询删除和查询所有