使用 man ascii 来查看 ASCII 表。

elasticsearch 如何聚合后计算,聚合后的值作为计算的条件

Elasticsearch | 作者 anchen | 发布于2021年03月22日 | 阅读数:648

现有存储有多张工单,每张工单有字段:客服人员、评价。评价有:低、中、高。若有每张单子 低 评价10元, 中 评价20元,高 评价30元。现希望算出每个客服人员的绩效。
 
我现在就能写出以下步骤
GET /wd_ticket_data2/_search
{
"size": 0,
"query": {
"bool": {
"filter": {
"term": {
"State.keyword": "closed successful"
}
},
"must": {
"exists": {
"field": "DynamicField_nanduxishu"
}
}
}
},
"aggs": {
"客服": {
"terms": {
"field": "customer.keyword"
},
"aggs": {
"评价": {
"terms": {
"field": "evaluate.keyword"
},
"aggs": {
"得分": {

}
}
}
}
}
}
}

 
对评价聚合之后,如何去判断不同的值,然后去乘以不同的分值。
 
试着写了一下
{
"bucket_script": {
"buckets_path": {
"count": "_count"
},
"script": "1"
}
}
能取到每组的数量,但是如何取到每组的key?
 
望不吝赐教
已邀请:

thewind

赞同来自: anchen FFFrp febmark lyes

聚合求值.png

# 创建索引
PUT workorder

# 写入数据
POST workorder/_doc
{
"state": "closed successful",
"DynamicField_nanduxishu": "1",
"customer": "Jack",
"evaluate": "low"
}

POST workorder/_doc
{
"state": "closed successful",
"DynamicField_nanduxishu": "1",
"customer": "Tom",
"evaluate": "high"
}

POST workorder/_doc
{
"state": "closed successful",
"DynamicField_nanduxishu": "1",
"customer": "Tom",
"evaluate": "low"
}


POST workorder/_doc
{
"state": "closed successful",
"DynamicField_nanduxishu": "1",
"customer": "Rachel",
"evaluate": "middle"
}

# 聚合的查询
POST workorder/_search
{
"size": 0,
"query": {
"bool": {
"filter": {
"term": {
"state.keyword": "closed successful"
}
},
"must": {
"exists": {
"field": "DynamicField_nanduxishu"
}
}
}
},
"aggs": {
"customerAggs": {
"terms": {
"field": "customer.keyword",
"size": 10
},
"aggs": {
"sumAggs": {
"sum": {
"script": {
"source": "params.get(doc['evaluate.keyword'].value)",
"params": {
"low": 10,
"middle": 20,
"high": 30
}
}
}
},
"detail": {
"terms": {
"field": "evaluate.keyword"
}
}
}
}
}
}

thewind

赞同来自:

哈哈,客气了,共同进步。

要回复问题请先登录注册