【发布时间】:2021-05-27 10:59:23
【问题描述】:
我想按字段对搜索结果进行分组。 示例:我有 userId 对应于多个用户名的数据。 因此,在搜索结果中,我想将所有 userId 及其对应的用户名分组。
目前使用聚合,我可以对 userId 进行分组,但无法检索其对应的用户名列表。 我得到以下信息。
"aggregations" : {
"by_user_id" : {
"after_key" : {
"group_by_search" : 2335
},
"buckets" : [
{
"key" : {
"group_by_search" : 2
},
"doc_count" : 2
},
{
"key" : {
"group_by_search" : 1000
},
"doc_count" : 4
},
{
"key" : {
"group_by_search" : 2335
},
"doc_count" : 2
}
]
}
我想要的是:
"aggregations" : {
"by_corp_id" : {
"after_key" : {
"group_by_search" : 2335
},
"buckets" : [
{
"key" : {
"group_by_search" : 2
"usernames":[1111,222] ***//this is list of usernames having same userId***
},
"doc_count" : 2
},
{
"key" : {
"group_by_search" : 1000
"usernames":[11 ,0101,1199,222] ***//this is list of usernames having same userId***
},
"doc_count" : 4
},
{
"key" : {
"group_by_search" : 2335
"usernames":[1111,222] ***//this is list of usernames having same userId***
},
"doc_count" : 2
}
]
}
有没有办法在 Elasticsearch 中使用聚合直接实现这一点?
更新:我正在使用以下聚合
"aggregations": {
"by_user_id": {
"composite": {
"size": 1000,
"sources": [
{
"group_by_search": {
"terms": {
"field": "user_id",
"missing_bucket": false,
"order": "asc"
}
}
}
]
}
}
}
谢谢。
【问题讨论】:
-
你能不能也提供你正在做的聚合查询?
-
@Val 我已经用聚合值更新了问题。
标签: java elasticsearch elasticsearch-aggregation