我刚打酱油去了,不好意思

有没老哥知道根据一个字段分组根据另一个字段聚合的玩法?

Elasticsearch | 作者 God_lockin | 发布于2019年04月15日 | 阅读数:2698

现在有这样一个index
 "mappings": {
"_doc": {
"properties": {
"userId": {
"type": "keyword"
},
"tag": {
"type": "nested",
"properties": {
"keyword": {
"type": "keyword"
},
"score": {
"type": "double"
}
}
}
}
}
}
需要对每个user的tag.keyword进行分组,然后对每个keyword里面对score取sum。
已邀请:

God_lockin

赞同来自:

histogram aggs 的聚合字段需要是数字型的
filter + agg的话可能要hardcode一大堆keyword
script/pipeline的话好像也没法正确分组,直接就sum出来个最终结果了

rochy - rochy_he

赞同来自:

对 userId 进行 term 聚合然后设置 tag.keyword 子聚合
最后对 tag.score 子聚合求 sum 即可

God_lockin

赞同来自:

表述有点问题,我要先按tag.keyword分组,然后对分组里每一个得分做匹配,不同的得分的乘不同的系数,但是下面这种聚合得不到最终结果,只有key和score分别count的结果
{
"size": 0,
"aggs": {
"nested": {
"nested": {
"path": "tag"
},
"aggs": {
"tag_keyword": {
"terms": {
"field": "tag.keyword"
},
"aggs": {
"tag_keyword_key": {
"terms": {
"field": "tag.keyword"
}
},
"tag_score": {
"terms": {
"field": "tag.
score"
}
},
"aggs": {
"bucket_script": {
"buckets_path": {
"agg_score": "tag_score.key",
"agg_key": "tag_keyword_key.key"
},
"script": "int rate = 1; if (params.agg_score == '1') {rate = 2}else if (params.agg_score == '3') {rate = 3} return params.score * rate;"
}
}
}
}
}
}
}
}
我期望的结果是:
[
{
"keyword":"test1",
"score": 10 // 1 * 2 + 2 * 4
},
{
"keyword":"test2",
"score": 18 // 2 * 3 + 3 * 4
}
]

要回复问题请先登录注册