我无法找到正确的语法来获取根据计数字段排序的子对象的聚合。
一个很好的例子是Twitter文档:
{
"properties" : {
"id" : {
"type" : "long"
},
"message" : {
"type" : "string"
},
"user" : {
"type" : "object",
"properties" : {
"id" : {
"type" : "long"
},
"screenName" : {
"type" : "string"
},
"followers" : {
"type" : "long"
}
}
}
}
}
如何获取一组推文的顶级影响者?这将是一个按“user.followers”字段排序的前10个“user”对象的唯一列表。
我尝试使用top_hits,但出现异常:
org.elasticsearch.common.breaker.CircuitBreakingException:[FIELDDATA] 数据太大,数据为[user.id]
"aggs": {
"top-influencers": {
"terms": {
"field": "user.id",
"order": {
"top_hit": "desc"
}
},
"aggs": {
"top_tags_hits": {
"top_hits": {}
},
"top_hit": {
"max": {
"field": "user.followers"
}
}
}
}
}
我可以使用查询的“sort”字段来几乎获得想要的内容(没有聚合),但是如果用户有多条推文,则他们将在结果中出现两次。我需要能够按子对象“用户”进行分组,并且只返回每个用户一次。
---更新---
我已经成功获取了非常快速的前用户列表。不幸的是,它仍然不是唯一的。此外,文档说top_hits被设计为子聚合...,而我正在将其用作顶级聚合...
"aggs": {
"top_influencers": {
"top_hits": {
"sort": [
{
"user.followers": {
"order": "desc"
}
}
],
"_source": {
"include": [
"user.id",
"user.screenName",
"user.followers"
]
},
"size": 10
}
}
}