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/
技術在於交流!