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 运行结果

4、完整代码

https://gitee.com/huan1993/spring-cloud-parent/tree/master/es/es8-api/src/main/java/com/huan/es8/aggregations/metric

5、参考文档

1、https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-metrics-max-aggregation.html

elasticsearch之metric聚合的更多相关文章

  1. Elasticsearch(8) --- 聚合查询(Metric聚合)

    Elasticsearch(8) --- 聚合查询(Metric聚合) 在Mysql中,我们可以获取一组数据的 最大值(Max).最小值(Min).同样我们能够对这组数据进行 分组(Group).那么 ...

  2. ElasticSearch实战系列五: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合

    Title:ElasticSearch实战系列四: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合 前言 在上上一篇中介绍了ElasticSearch实战系列三: Elas ...

  3. Elasticsearch Metric聚合

    首先查看index文档信息 $ curl -XGET "http://172.16.101.55:9200/_cat/indices?v" 输出 health status ind ...

  4. java使用elasticsearch分组进行聚合查询(group by)-项目中实际应用

    java连接elasticsearch 进行聚合查询进行相应操作 一:对单个字段进行分组求和 1.表结构图片: 根据任务id分组,分别统计出每个任务id下有多少个文字标题 .SQL:select id ...

  5. elasticsearch 权威指南聚合阅读笔记(七)

    count(1) select clssId,count(1) from student group by  classId { "size":0, "aggs" ...

  6. 读《深入理解Elasticsearch》点滴-聚合-top_hits

    以下是官网手册(部分)(v5.1) 直接直接看官网手册 https://www.elastic.co/guide/en/elasticsearch/reference/5.1/search-aggre ...

  7. python 使用 elasticsearch 常用方法(聚合)

    #记录聚合查询方法 from elasticsearch import Elasticsearch es = Elasticsearch(['xx.xx.xx.xx:9200']) #获取最小的年龄r ...

  8. 小试牛刀ElasticSearch大数据聚合统计

    ElasticSearch相信有不少朋友都了解,即使没有了解过它那相信对ELK也有所认识E即是ElasticSearch.ElasticSearch最开始更多用于检索,作为一搜索的集群产品简单易用绝对 ...

  9. Elasticsearch强大的聚合功能Facet

    在常规数据库中,我们都知道有一个sql就是group,分组.如果主表只有对应的一个列记录的分组的ID,那么还好统计,比如说每本书book表,有一个分类catId,记录是属于哪一类的书,那么直接按照ca ...

  10. elasticsearch按范围聚合

    范围聚合适用于对数据按照指定范围进行分类聚合的应用场景,from和to表示范围起始值的半开半闭区间(包含from值不包含to值),key表示别名 如 {:key=>"2h", ...

随机推荐

  1. PHP全栈开发(八):CSS Ⅴ 超链接 style

    CSS里面有专门针对超链接的选择器,也就是他们: a:link - 正常,未访问过的链接 a:visited - 用户已访问过的链接 a:hover - 当用户鼠标放在链接上时 a:active - ...

  2. python流程控制下-for、while循环补充

    循环结构之for循环 实现循环结构还可以用关键字for. for关键字 我们来看这一段代码: emotions = ['smile', 'laugh', 'cry', 'angry'] for emo ...

  3. C++编程范式(函数)

    1 // 2 // main.cpp 3 // test 4 // 5 // Created by Shaojun on 30/5/2020. 6 // Copyright 2020 Shaojun. ...

  4. 关于针对XSS漏洞攻击防范的一些思考

    众所周知,XSS几乎在最常见.危害最大的WEB漏洞.针对这个危害,我们应该怎么防范呢. 下面简单说一下思路. 作者:轻轻的烟雾(z281099678) 一.XSS漏洞是什么 XSS漏洞网上的资料太多, ...

  5. k8s集权IP更换

    -.背景描述 背景:在场内进行部署完成后标准版产品,打包服务器到客户现场后服务不能正常使用,因为客户现场的IP地址不能再使用场内的IP,导致部署完的产品环境在客户现场无法使用:此方案就是针对这一问题撰 ...

  6. Element Ui 安装以及配置

    npm 安装 推荐使用 npm 的方式安装,它能更好地和 webpack 打包工具配合使用. npm i element-ui -S 引入 Element 你可以引入整个 Element,或是根据需要 ...

  7. @confirguration(proxyBeanMethods = false)的作用,如何选择Full模式和Lite模式

    @Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件 public class MyConfig { @Bean ...

  8. Istio Ambient Mesh七层服务治理图文详解

    摘要:本文主要集中剖析Ambient mesh七层服务治理相关内容. 本文分享自华为云社区<Istio Ambient Mesh七层服务治理图文详解>,作者:华为云云原生团队. 由于Amb ...

  9. 系统启动后bond配置不生效问题定位

    背景描述 为了适配新功能,裸金属服务的磁盘镜像中做了如下修改: dracut添加network, iscsi模块 grub添加rd.iscsi.firmware=1参数 删除网卡配置文件/etc/sy ...

  10. 带你了解S12直播中的“黑科技”

    摘要:让精彩更流畅.让较量更清晰.让参与更沉浸.让体验更有趣,幕后的舞台,从来都是技术的战场,S12背后的名场面同样场场高能. 本文分享自华为云社区<用硬核方式打开S12名场面>,作者:华 ...