GET chatsession/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"char2cust": {
"gte": 10
}
}
}
]
}
},
"size": 0,
"aggs": {
"uniq_user": {
"terms": {
"field": "user_name",
"size": 99999
},
"aggs": {
"sum_char2cust": {
"sum": {
"field": "char2cust"
}
},
"sum_cust2char": {
"sum": {
"field": "cust2char"
}
}
}
}
}
}
我想要sum_char2ust和sum_cust2char仅用于前20个文档独特的用户。我尝试过使用top_hits和其他解决方案,但没有
下面的查询应该会给出预期的结果
GET chatsession/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"char2cust": {
"gte": 10
}
}
}
]
}
},
"size": 0,
"aggs": {
"uniq_user": {
"terms": {
"field": "user_name",
"size": 20 --> Note here.
},
"aggs": {
"sum_char2cust": {
"sum": {
"field": "char2cust"
}
},
"sum_cust2char": {
"sum": {
"field": "cust2char"
}
}
}
}
}
}
为了获得char2cust
和cust2char
字段的和,对于前20个用户(基于他们的id),您需要根据Max aggregation结果对术语聚合结果进行排序。
试试下面这个查询:
{
"query": {
"bool": {
"filter": [
{
"range": {
"char2cust": {
"gte": 10
}
}
}
]
}
},
"size": 0,
"aggs": {
"uniq_user": {
"terms": {
"field": "user_name",
"size": 20,
"order": {
"latestOrder": "desc"
}
},
"aggs": {
"sum_char2cust": {
"sum": {
"field": "char2cust"
}
},
"sum_cust2char": {
"sum": {
"field": "cust2char"
}
},
"latestOrder": {
"max": {
"field": "id"
}
}
}
}
}
}