java对es做聚合查询_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(&

你可能感兴趣的:(java对es做聚合查询)