1、terms分组查询
//分组聚合 @Test public void test40() 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)); //以年龄分组,组名为:terms AggregationBuilder builder = AggregationBuilders.terms("terms").field("age"); SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).execute().actionGet(); Terms terms = response.getAggregations().get("terms"); for(Terms.Bucket term:terms.getBuckets()) { System.out.println(term.getKey()+" "+term.getDocCount()); } }
2、terms query聚合查询
//filter聚合 @Test public void test41() 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)); //以年龄分组,并查询年龄为20的有多少人 QueryBuilder queryBuilder = QueryBuilders.termQuery("age", 20); AggregationBuilder builder = AggregationBuilders.filter("filter",queryBuilder); SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).execute().actionGet(); Filter filter = response.getAggregations().get("filter"); System.out.println(filter.getDocCount()); }
3、filters聚合
//filters聚合:指定多个过滤条件 @Test public void test42() 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)); //查询heijiu changge中各有多少个文档 QueryBuilder queryBuilder = QueryBuilders.termQuery("interests", "hejiu"); QueryBuilder queryBuilder2 = QueryBuilders.termQuery("interests", "changge"); AggregationBuilder builder = AggregationBuilders.filters("filters",queryBuilder,queryBuilder2); SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).execute().actionGet(); Aggregation filter = response.getAggregations().get("filters"); System.out.println(filter.toString()); }
4、range聚合
//range聚合:范围分组过滤条件 @Test public void test43() 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)); //统计年龄在50以下的,年龄在25至50之间,年龄在25以上的人数 AggregationBuilder builder = AggregationBuilders .range("range") .field("age") .addUnboundedTo(50) .addRange(25,50) .addUnboundedFrom(25); SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).execute().actionGet(); Aggregation filter = response.getAggregations().get("range"); System.out.println(filter.toString()); }
5、missing聚合
//missing聚合:为空聚合统计 @Test public void test44() 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)); //统计价格为空的个数 AggregationBuilder builder = AggregationBuilders.missing("missing").field("price"); SearchResponse response = client.prepareSearch("lib4").addAggregation(builder).execute().actionGet(); Aggregation filter = response.getAggregations().get("missing"); System.out.println(filter.toString()); }
下一篇博客本人将书写java操作elasticsearch实现集群管理。对后期博客感兴趣的朋友可以关注交流,转发请说明出处,本人的博客地址为:https://www.cnblogs.com/chenyuanbo/
技术在于交流!