• (25)ElasticSearch java项目中聚合查询terms、filter、filters、range、missing


      1、按年龄分组,查询每组的个数

    @Test
        public void testTerms() 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));
            //创建查询对象,按年龄分组,查询每组的个数
            AggregationBuilder agg = AggregationBuilders.terms("terms").field("age");
            //执行查询
            SearchResponse sr = client.prepareSearch("lib3")
                                      .addAggregation(agg)
                                      .execute()
                                      .actionGet();
            //获取结果
            Terms terms = sr.getAggregations().get("terms");
            //遍历
            for(Terms.Bucket entry:terms.getBuckets()) {
                System.out.println(entry.getKey()+"----------"+entry.getDocCount());
            }
            client.close();
       }

      2、过滤查询age字段是20的文档

    @Test
        public void testFilter() 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));
            //指定查询条件,age字段是20的
            QueryBuilder query = QueryBuilders.termQuery("age", 29);
            //过滤查询条件,过滤出满足查询条件的文档
            AggregationBuilder agg = AggregationBuilders.filter("filter", query);
            //执行查询
            SearchResponse sr = client.prepareSearch("lib3")
                                      .addAggregation(agg)
                                      .execute()
                                      .actionGet();
            //获取结果
            Filter filter = sr.getAggregations().get("filter");
            //输出个数
            System.out.println(filter.getDocCount());
            client.close();
       }

      3、过滤查询interests字段包含changge、hejiu的文档

    @Test
        public void testFilters() 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));
            //指定查询条件,interests字段包含changge的
            QueryBuilder query = QueryBuilders.termQuery("interests", "changge");
            //指定查询条件,interests字段包含hejiu的
            QueryBuilder query2 = QueryBuilders.termQuery("interests", "hejiu");
            //过滤查询条件,过滤出满足query或者query2的文档
            AggregationBuilder agg = AggregationBuilders.filters("filters",
                                                                new FiltersAggregator.KeyedFilter("条件1",query),
                                                                new FiltersAggregator.KeyedFilter("条件2",query2));
            //执行查询
            SearchResponse sr = client.prepareSearch("lib3")
                                      .addAggregation(agg)
                                      .execute()
                                      .actionGet();
            //获取结果
            Filters filters = sr.getAggregations().get("filters");
            //遍历,指定了两个filter,所以输出两条数据,key分别是条件1、条件2
            for(Filters.Bucket entry:filters.getBuckets()) {
                System.out.println(entry.getKey()+"----------"+entry.getDocCount());
            }
            client.close();
       }

      4、分别统计年龄小于50的,25到50之间的,大于25的文档个数

    @Test
        public void testRange() 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));
            //统计3组数据年龄小于50的,25到50之间的,大于25的
            AggregationBuilder agg = AggregationBuilders.range("range")
                                                        .field("age")
                                                        .addUnboundedTo(50)//( ,to)
                                                        .addRange(25,50)//[from,to)
                                                        .addUnboundedFrom(25);//[from, )
            //执行查询
            SearchResponse sr = client.prepareSearch("lib3")
                                      .addAggregation(agg)
                                      .execute()
                                      .actionGet();
            //获取结果
            Range r = sr.getAggregations().get("range");
            //遍历,输出3个key,*-50.0;25.0-50.0;25.0-*
            for(Range.Bucket entry:r.getBuckets()) {
                System.out.println(entry.getKey()+"----------"+entry.getDocCount());
            }
            client.close();
       }

      5、统计缺失age字段的文档个数

    @Test
        public void testMissing() 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));
            //统计缺失字段age的文档
            AggregationBuilder agg = AggregationBuilders.missing("missing").field("age");
            //执行查询
            SearchResponse sr = client.prepareSearch("lib3")
                                      .addAggregation(agg)
                                      .execute()
                                      .actionGet();
            //获取结果
            Aggregation aggregation = sr.getAggregations().get("missing");
            //输出结果
            System.out.println(aggregation.toString());
            client.close();
       }
  • 相关阅读:
    快递标示
    git 操作命令系列
    在线js调试地址
    jQuery 的 validator 验证动态添加的信息
    mysql批量插入
    array_map 批量对数据执行某个自定义方法
    使用 header函数实现文件下载
    设置UTF-8 编码
    常用短信接口平台
    async: false 实现AJAX同步请求 ( $.ajax同步/异步(async:false/true) )
  • 原文地址:https://www.cnblogs.com/javasl/p/12081785.html
Copyright © 2020-2023  润新知