zoukankan      html  css  js  c++  java
  • java操作elasticsearch实现组合桶聚合

    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/

    技术在于交流!

  • 相关阅读:
    java连接常见数据库的连接字符串
    一个用来自动管理大容量表的Sql脚本
    Ibatis2.0使用说明(一)——入门实例篇
    态度决定你的人生高度
    jboss配置入门(二) -JBOSS3.2.3/3.2.6部署及配置修改
    如何在一个工程里面实现不同的功能打入不同的日志文件中【log4net】
    Xsl实践总结(一)
    Xsl实践总结(三)-介绍一款开发XSL不错的IDE(Stylus)
    Xsl实践总结(二)
    教你节省时间 让你个人效率翻三倍
  • 原文地址:https://www.cnblogs.com/chenyuanbo/p/10296854.html
Copyright © 2011-2022 走看看