详解elasticsearch之metric聚合实现示例

 更新时间:2023年01月16日 09:09:07   作者:huan1993  
这篇文章主要为大家介绍了elasticsearch之metric聚合实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

1、背景

此篇文章简单的记录一下 elasticsearchmetric聚合操作。比如求 平均值、最大值、最小值、求和、总计、去重总计等。

2、准备数据

2.1 准备mapping

PUT /index_person
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "id":{
        "type": "long"
      },
      "name": {
        "type": "keyword"
      },
      "age": {
        "type": "integer"
      },
      "class":{
        "type": "text",
        "fielddata": true
      },
      "province":{
        "type": "keyword"
      }
    }
  }
}

2.2 准备数据

PUT /index_person/_bulk
{"index":{"_id":1}}
{"id":1, "name":"张三","age":18,"class":"大一班","province":"湖北"}
{"index":{"_id":2}}
{"id":2, "name":"李四","age":19,"class":"大一班","province":"湖北"}
{"index":{"_id":3}}
{"id":3, "name":"王武","age":20,"class":"大二班","province":"北京"}
{"index":{"_id":4}}
{"id":4, "name":"赵六","age":21,"class":"大三班技术班","province":"北京"}
{"index":{"_id":5}}
{"id":5, "name":"钱七","age":22,"class":"大三班","province":"湖北"}

3、metric聚合

3.1 max 平均值

3.1.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "max": {
        "field": "age",
        "missing": 10
      }
    }
  }
}
POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "max": {
        "script": {
          "lang": "painless",
          "source": """
            doc.age
          """
        }
      }
    }
  }
}
POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "max": {
        "field": "age", 
        "script": {
          "lang": "painless",
          "source": """
            _value * params.a
          """,
          "params": {
            "a": 2
          }
        }
      }
    }
  }
}

3.1.2 java代码

@Test
@DisplayName("最大值聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.max(max ->
                                    // 聚合的字段
                                    max.field("age")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}
@Test
@DisplayName("脚本聚合")
public void test02() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.max(max ->
                                    max.script(script ->
                                            script.inline(inline ->
                                                    inline.lang(ScriptLanguage.Painless)
                                                            // 脚本表达式
                                                            .source("doc.age")
                                            )
                                    )
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}
@Test
@DisplayName("值脚本聚合")
public void test03() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.max(max ->
                                    // 指定参与聚合的字段
                                    max.field("age")
                                            .script(script ->
                                                    script.inline(inline ->
                                                            inline.lang(ScriptLanguage.Painless)
                                                                    // 脚本表达式
                                                                    .source("_value * params.plus")
                                                                    // 参数
                                                                    .params("plus", JsonData.of(2))
                                                    )
                                            )
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.2 min最小值

3.2.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "min": {
        "field": "age",
        "missing": 10
      }
    }
  }
}

3.2.2 java

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "min": {
        "field": "age",
        "missing": 10
      }
    }
  }
}

3.3 min最小值

3.3.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "avg": {
        "field": "age",
        "missing": 10
      }
    }
  }
}

3.3.2 java

@Test
@DisplayName("平均值聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.avg(avg ->
                                    // 聚合的字段
                                    avg.field("age")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.4 min最小值

3.4.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "sum": {
        "field": "age",
        "missing": 10
      }
    }
  }
}

3.4.2 java

@Test
@DisplayName("求和聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.sum(sum ->
                                    // 聚合的字段
                                    sum.field("age")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.5 count(*)

3.5.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "value_count": {
        "field": "province",
        "missing": 10
      }
    }
  }
}

3.5.2 java

@Test
@DisplayName("count(*)聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.valueCount(valueCount ->
                                    // 聚合的字段
                                    valueCount.field("age")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.6 count(distinct)

3.6.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "cardinality": {
        "field": "province",
        "missing": 10
      }
    }
  }
}

3.6.2 java

@Test
@DisplayName("count(distinct)聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.cardinality(cardinality ->
                                    // 聚合的字段
                                    cardinality.field("province")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.7 stat (max,min,avg,count,sum)

3.7.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "stats": {
        "field": "avg",
        "missing": 10
      }
    }
  }
}

3.7.2 java

@Test
@DisplayName("stat聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.stats(stats ->
                                    // 聚合的字段
                                    stats.field("age")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.8 聚合后返回每个聚合涉及的文档

3.8.1 需求

根据 province进行terms聚合,然后获取每个terms聚合 age最大的那个文档。

3.8.2 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "range": {
      "age": {
        "gte": 10
      }
    }
  },
  "aggs": {
    "agg_01": {
      "terms": {
        "field": "province"
      },
      "aggs": {
        "agg_02": {
          "top_hits": {
            "from": 0,
            "size": 1,
            "sort": [
              {
                "age": {"order": "desc"}
              }
            ],
            "_source": {
              "includes": ["id","age","name"]
            }
          }
        }
      }
    }
  }
}

3.8.3 java

@Test
@DisplayName("top hits 聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .query(query -> query.range(range -> range.field("age").gt(JsonData.of(10))))
                    .aggregations("agg_01", agg ->
                            agg.terms(terms ->
                                            terms.field("province")
                                    )
                                    .aggregations("agg_02", subAgg ->
                                            subAgg.topHits(topHits ->
                                                    topHits.from(0)
                                                            .size(1)
                                                            .sort(sort -> sort.field(field -> field.field("age").order(SortOrder.Desc)))
                                                            .source(source -> source.filter(filter -> filter.includes(Arrays.asList("id", "age", "name"))))
                                            )
                                    )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.8.4 运行结果

完整代码

gitee.com/huan1993/sp…

参考文档

1、www.elastic.co/guide/en/el…

以上就是详解elasticsearch之metric聚合实现示例的详细内容,更多关于elasticsearch metric聚合的资料请关注脚本之家其它相关文章!

相关文章

  • 在Java中按值调用和按引用调用

    在Java中按值调用和按引用调用

    这篇文章主要介绍了Java中的按值调用和按引用调用,一种是按值调用,另一种是按引用调用,这两种方式通常根据作为输入或参数传递给它们的值的类型来区分,下文相关的更多详细资料感兴趣的小伙伴可以参考一下
    2022-04-04
  • Java跨平台原理与虚拟机相关简介

    Java跨平台原理与虚拟机相关简介

    这篇文章主要介绍了Java跨平台原理与虚拟机的相关资料,帮助大家更好的理解和学习使用Java,感兴趣的朋友可以了解下
    2021-03-03
  • Java设计模式之观察者模式(Observer模式)

    Java设计模式之观察者模式(Observer模式)

    这篇文章主要介绍了Java设计模式之观察者模式(Observer模式),文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • Java join 线程控制用法

    Java join 线程控制用法

    Java join 线程控制用法,需要的朋友可以参考一下
    2013-03-03
  • springboot cloud使用eureka整合分布式事务组件Seata 的方法

    springboot cloud使用eureka整合分布式事务组件Seata 的方法

    这篇文章主要介绍了springboot cloud使用eureka整合分布式事务组件Seata 的方法 ,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-05-05
  • Spring定时任务实现与配置(一)

    Spring定时任务实现与配置(一)

    这篇文章主要为大家详细介绍了Spring定时任务的实现与配置第一篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Java实现经典游戏复杂迷宫

    Java实现经典游戏复杂迷宫

    这篇文章主要介绍了如何利用java语言实现经典《复杂迷宫》游戏,文中采用了swing技术进行了界面化处理,感兴趣的小伙伴可以动手试一试
    2022-02-02
  • 使用ThreadPoolExecutor之高效处理并发任务

    使用ThreadPoolExecutor之高效处理并发任务

    这篇文章主要介绍了使用ThreadPoolExecutor之高效处理并发任务,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • 一文搞懂spring boot本地事务@Transactional参数

    一文搞懂spring boot本地事务@Transactional参数

    这篇文章主要介绍了spring boot本地事务@Transactional参数详解,本文通过示例代码图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-10-10
  • java 读取zip文件的两种方式示例详解

    java 读取zip文件的两种方式示例详解

    ZIP(压缩文件)是一种常见的文件格式,在Java中可以使用java.util.zip包提供的API来读取和处理ZIP文件,本文将介绍如何使用Java读取ZIP文件,并提供代码示例,感兴趣的朋友跟随小编一起看看吧
    2024-07-07

最新评论