【发布时间】:2019-07-09 03:31:36
【问题描述】:
我无法找到正确的语法来获取按计数字段排序的子对象的聚合。
Twitter 文档就是一个很好的例子:
{
"properties" : {
"id" : {
"type" : "long"
},
"message" : {
"type" : "string"
},
"user" : {
"type" : "object",
"properties" : {
"id" : {
"type" : "long"
},
"screenName" : {
"type" : "string"
},
"followers" : {
"type" : "long"
}
}
}
}
}
我将如何获得一组给定推文的顶级影响者?这将是按“user.followers”字段排序的前 10 个“用户”对象的唯一列表。
我尝试过使用 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"
}
}
}
}
}
我几乎可以使用查询中的“排序”字段(无聚合)得到我想要的,但是如果用户有多个推文,那么它们将在结果中出现两次。我需要能够按子对象“用户”进行分组,并且每个用户只返回一次。
---更新---
我已经设法获得了一份及时返回的顶级用户列表。不幸的是,它仍然不是独一无二的。文档还说 top_hits 被设计为子 agg...,我将其用作顶级 agg...
"aggs": {
"top_influencers": {
"top_hits": {
"sort": [
{
"user.followers": {
"order": "desc"
}
}
],
"_source": {
"include": [
"user.id",
"user.screenName",
"user.followers"
]
},
"size": 10
}
}
}
【问题讨论】:
标签: elasticsearch