1、背景

此篇文档简单的记录一下在es使用bucket script来进行聚合的一个例子。

2、需求

假设我们有一个简单的卖车数据,记录每个月month在卖了brand品牌的车salesVolume的数量。

此处我们需要聚合出 每个月brand=宝马的车在每个月销售占比

3、准备数据

3.1 mapping

PUT /index_bucket_script
{
"mappings": {
"properties": {
"month": {
"type": "keyword"
},
"brand": {
"type": "keyword"
},
"salesVolume": {
"type": "integer"
}
}
}
}

3.2 插入数据

PUT /index_bucket_script/_bulk
{"index":{"_id":1}}
{"month":"2023-01","brand":"宝马","salesVolume":100}
{"index":{"_id":3}}
{"month":"2023-02","brand":"大众","salesVolume":80}
{"index":{"_id":4}}
{"month":"2023-02","brand":"宝马","salesVolume":20}

注意: 此处2023-02月份的数据插入了2个品牌的数据。

4、bucket_script聚合的语法

5、聚合

5.1 根据月份分组排序

GET index_bucket_script/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"根据月份分组": {
"terms": {
"field": "month",
"order": {
"_key": "asc"
}
}
}
}
}

5.2 统计每个月卖了多少辆车

GET index_bucket_script/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"根据月份分组": {
"terms": {
"field": "month",
"order": {
"_key": "asc"
}
},
"aggs": {
"统计每个月卖了多少辆车": {
"sum": {
"field": "salesVolume"
}
}
}
}
}
}

5.3 统计每个月卖了多少宝马车

GET index_bucket_script/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"根据月份分组": {
"terms": {
"field": "month",
"order": {
"_key": "asc"
}
},
"aggs": {
"统计每个月卖了多少辆车": {
"sum": {
"field": "salesVolume"
}
},
"统计每个月卖了多少宝马车": {
"filter": {
"term": {
"brand": "宝马"
}
},
"aggs": {
"每个月卖出的宝马车辆数": {
"sum": {
"field": "salesVolume"
}
}
}
}
}
}
}
}

5.4 每个月宝马车销售占比

5.4.1 dsl

GET index_bucket_script/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"根据月份分组": {
"terms": {
"field": "month",
"order": {
"_key": "asc"
}
},
"aggs": {
"统计每个月卖了多少辆车": {
"sum": {
"field": "salesVolume"
}
},
"统计每个月卖了多少宝马车": {
"filter": {
"term": {
"brand": "宝马"
}
},
"aggs": {
"每个月卖出的宝马车辆数": {
"sum": {
"field": "salesVolume"
}
}
}
},
"每个月宝马车销售占比": {
"bucket_script": {
"buckets_path": {
"fenzi": "统计每个月卖了多少宝马车 > 每个月卖出的宝马车辆数",
"fenmu": "统计每个月卖了多少辆车"
},
"script": "params.fenzi / params.fenmu * 100"
}
}
}
}
}
}

5.4.2 java

@Test
@DisplayName("统计宝马车每个月销售率")
public void test01() throws IOException {
SearchRequest request = SearchRequest.of(searchRequest ->
searchRequest.index(INDEX_PERSON)
.query(query -> query.matchAll(matchAll -> matchAll))
.size(0)
.aggregations("根据月份分组", monthAggr ->
monthAggr.terms(terms -> terms.field("month").order(
NamedValue.of("_key", SortOrder.Asc)
))
.aggregations("统计每个月卖了多少辆车", agg1 ->
agg1.sum(sum -> sum.field("salesVolume"))
)
.aggregations("统计每个月卖了多少宝马车", agg2 ->
agg2.filter(filter -> filter.term(term -> term.field("brand").value("宝马")))
.aggregations("每个月卖出的宝马车辆数", agg3 ->
agg3.sum(sum -> sum.field("salesVolume"))
)
)
.aggregations("每个月宝马车销售占比", rateAggr ->
rateAggr.bucketScript(bucketScript ->
bucketScript.bucketsPath(path ->
path.dict(
new HashMap<String, String>() {
{
put("fenzi", "统计每个月卖了多少宝马车>每个月卖出的宝马车辆数");
put("fenmu", "统计每个月卖了多少辆车");
}
}
) )
.script(script ->
script.inline(inline -> inline.source("params.fenzi/params.fenmu"))
)
.format("#%")
)
)
)
);
System.out.println("request: " + request);
SearchResponse<String> response = client.search(request, String.class);
System.out.println("response: " + response);
}

5.4.3 运行结果

5、完整代码

https://gitee.com/huan1993/spring-cloud-parent/blob/master/es/es8-api/src/main/java/com/huan/es8/aggregations/pipeline/BucketScript统计宝马车每个月销售率.java

6、参考文档

1、https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#buckets-path-syntax

2、https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html

3、https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/DecimalFormat.html

最新文章

  1. Gyro
  2. guava之Joiner 和 Splitter
  3. js 性能基准测试工具-告别可能、也许、大概这样更快更省
  4. (Design Pattern) Singleton.
  5. Javascript中parentNode的用法
  6. WCF学习心得--客户端获取服务端自定义类数据
  7. erlang mnesia数据库设置主键自增
  8. Python 内置函数 range的使用
  9. Django - Django框架 简单介绍
  10. spring的bean是在什么时候实例化的
  11. 基本数据类型 int float str
  12. 使用Mongo进行分页
  13. MUI动态生成轮播图片
  14. 转:Newtonsoft.Json高级用法
  15. Windows7安装CodeTyphon
  16. 国内maven仓库地址
  17. 初中级web前端工程师的面试题分享
  18. [c#]WebClient异步下载文件并显示进度
  19. Keras-图片预处理
  20. ZOJ 1049 判断坐标点

热门文章

  1. Python基础阶段总结:ATM项目实战
  2. JavaWeb2
  3. 搜索&quot;xxxx&quot;的进程,同时杀进程
  4. day14-HTTP01
  5. 关于Intent.setDataAndType参数问题
  6. 利用KubeEdge在A500部署边缘推理任务
  7. 【Java集合框架001】为什么重写equals就要重写hashcode?
  8. 在Java Web中setContentType与setCharacterEncoding中设置字符编码格式的区别
  9. JavaEE Day12 Xml
  10. Navicat破解教程