zoukankan      html  css  js  c++  java
  • (22)ElasticSearch java项目中聚合运算求最大、最小、平均、求和以及求不同值

      1、求索引lib3下的age字段的最大值

    @Test
        public void testAggMax() 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));
            //aggMax是固定写法,求最大年龄
            AggregationBuilder agg = AggregationBuilders.max("aggMax").field("age");
            //执行查询
            SearchResponse response = client.prepareSearch("lib3")//索引是lib
                                      .addAggregation(agg)
                                      .get();
            //获取结果
            Max max = response.getAggregations().get("aggMax");
            //输出结果
            System.out.println(max.getValue());
            client.close();
       }

      2、求索引lib3下的age字段的最小值

    @Test
        public void testAggMin() 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));
            //aggMin是固定写法,求最小年龄
            AggregationBuilder agg = AggregationBuilders.min("aggMin").field("age");
            //执行查询
            SearchResponse response = client.prepareSearch("lib3")//索引是lib
                                      .addAggregation(agg)
                                      .get();
            //获取结果
            Min min = response.getAggregations().get("aggMin");
            //输出结果
            System.out.println(min.getValue());
            client.close();
       }

      3、求索引lib3下的age字段的平均值

    @Test
        public void testAggAvg() 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));
            //aggAvg是固定写法,求平均年龄
            AggregationBuilder agg = AggregationBuilders.avg("aggAvg").field("age");
            //执行查询
            SearchResponse response = client.prepareSearch("lib3")//索引是lib
                                      .addAggregation(agg)
                                      .get();
            //获取结果
            Avg avg = response.getAggregations().get("aggAvg");
            //输出结果
            System.out.println(avg.getValue());
            client.close();
       }

      4、求索引lib3下的age字段的和

    @Test
        public void testAggSum() 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));
            //aggSum是固定写法,求年龄总和
            AggregationBuilder agg = AggregationBuilders.sum("aggSum").field("age");
            //执行查询
            SearchResponse response = client.prepareSearch("lib3")//索引是lib
                                      .addAggregation(agg)
                                      .get();
            //获取结果
            Sum sum = response.getAggregations().get("aggSum");
            //输出结果
            System.out.println(sum.getValue());
            client.close();
       }

      5、求索引lib3中age字段有多个不同值

    @Test
        public void testAggCardinality() 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));
            //aggCardinality是固定写法,求某字段有多个不同值
            AggregationBuilder agg = AggregationBuilders.cardinality("aggCardinality").field("age");
            //执行查询
            SearchResponse response = client.prepareSearch("lib3")//索引是lib
                                      .addAggregation(agg)
                                      .get();
            //获取结果
            Cardinality cardinality = response.getAggregations().get("aggCardinality");
            //输出结果
            System.out.println(cardinality.getValue());
            client.close();
       }

     

  • 相关阅读:
    哈夫曼树
    粒子群算法(1)----粒子群算法简单介绍
    白话经典算法系列之七 堆与堆排序
    UVa 11879
    有计划,公司运行——写到犹豫大三女生
    _00013 一致性哈希算法 Consistent Hashing 新的讨论,并出现相应的解决
    s3c2440的A/D转换应用
    iSwifting如何发送照片社区
    【Android开发经验】使用反射,得到的类的字段、方法、并实现了简单的调用
    于Unity3D动态创建对象和创建Prefab三种方式的原型对象
  • 原文地址:https://www.cnblogs.com/javasl/p/12070429.html
Copyright © 2011-2022 走看看