惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
The Cloudflare Blog
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
Jina AI
Jina AI
V
V2EX
罗磊的独立博客
V
Visual Studio Blog
A
About on SuperTechFans
IT之家
IT之家
P
Proofpoint News Feed
B
Blog
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
elasticsearch常用script聚合
祈雨的笔记 · 2019-05-14 · via 祈雨的笔记

keyword返回的数据类型为org.elasticsearch.index.fielddata.ScriptDocValues,继承AbstractList是一个数组。常用api有getValue()获取数组第一个值;get(int index)获取数值第x个值。

ScriptDocValues

Modifier and Type Class and Description
static class ScriptDocValues.Booleans
static class ScriptDocValues.BytesRefs
static class ScriptDocValues.Dates
static class ScriptDocValues.Doubles
static class ScriptDocValues.GeoPoints
static class ScriptDocValues.Longs
static class ScriptDocValues.Strings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
GET /user_profile_v2/_search
{
"size": 0,
"query": {
"bool": {
"must": [{
"match": {
"merchant_code.keyword": "jt20180528102410333649"
}
}]
}
},
"aggs": {
"birth_month_count": {
"scripted_metric": {
"init_script" : "params._agg.transactions = 0",
"map_script" : "String value = (doc['birth_month.keyword']==null?'':doc['birth_month.keyword'].getValue());boolean flag = value !='';if(flag){params._agg.transactions++}",
"combine_script" : "return params._agg.transactions",
"reduce_script" : "int result = 0;for(t in params._aggs) {result += t} return result"
}
},
"birth_month_list": {
"scripted_metric": {
"init_script" : "params._agg.transactions = []",
"map_script" : "String value = (doc['birth_month.keyword']==null?'':doc['birth_month.keyword'].getValue());boolean flag = value !='';if(flag && !params._agg.transactions.contains(value)){params._agg.transactions.add(value)}",
"combine_script" : "return params._agg.transactions",
"reduce_script" : "List result = []; for (int i=0;i<params._aggs.length;i++) {for (t in params._aggs[i]){if(!result.contains(t)){result.add(t)}}} return result"
}
},
"birth_month_map": {
"scripted_metric": {
"init_script" : "params._agg.transactions = [:]",
"map_script" : "String value = (doc['birth_month.keyword']==null?'':doc['birth_month.keyword'].getValue());boolean flag = value !='';if(flag && !params._agg.transactions.containsKey(value)){params._agg.transactions.put(doc['card_no.keyword'].getValue(),value)}",
"combine_script" : "return params._agg.transactions",
"reduce_script" : "Map result = [:]; for (int i=0;i<params._aggs.length;i++) {for (key in params._aggs[i].keySet()){if(!result.containsKey(key)){result.put(key,params._aggs[i].get(key))}}} return result"
}
}
}
}

更新

基于script的统计速度慢,对于count场景的统计直接使用批量查询替代,如下:

1
2
3
4
5
POST /_msearch?rest_total_hits_as_int=true
{"index":"user_profile_v2"}
{"size":0,"query":{"bool":{"must":[{"term":{"merchant_code":{"value":"jt20191017175015910985"}}}],"should":[{"terms":{"fans_type.keyword":["公众号粉丝"]}}],"minimum_should_match":1}}}
{"index":"user_profile_v2"}
{"size":0,"query":{"bool":{"must":[{"term":{"merchant_code":{"value":"jt20191017175015910985"}}}],"should":[{"terms":{"app_scope.keyword":["会员"]}}],"minimum_should_match":1}}}