我的数据对象包含 3 个字符串字段:部分、类别和用户名。 我想按类别和用户名进行分组,并找出前 20 个“部分”及其在我所做的分组中的计数
基本上我想要的结果就像sql查询: 选择部分,按类别从表组中计数(*),用户名顺序按 2 降序限制 20
我尝试对部分、类别和用户名进行聚合。结果是我正在寻找的分组,但它没有按此分组对“部分”字段进行排序(例如,我需要第一个结果来包含出现在类别和用户名及其计数的最多组合中的部分)
相反,它仅按其 doc_count 对部分进行排序,并且在每个部分内部,按其 doc_count 对“类别”进行排序,并在其中按其 doc_count 对每个“用户名”进行排序
这是我尝试过的:
GET some_index/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"match": {
"some_filter_i_needed": "filter_value"
}
}
]
}
},
"aggs": {
"top_section": {
"terms": {
"field": "section.keyword",
"size": 20,
"order": {
"_count": "desc"
}
},
"aggs": {
"top_category": {
"terms": {
"field": "category.keyword",
"size": 1,
"order": {
"_count": "desc"
}
},
"aggs": {
"top_username": {
"terms": {
"field": "username.keyword",
"size": 1 ,
"order": {
"_count": "desc"
}
}
}
}
}
}
}
}
}
有什么方法可以获得我正在寻找的输出吗?
谢谢
据我从您的问题中了解到,您需要首先按“类别”和“用户名”进行分组,然后找到每个组中顶部的“部分”。您提供的查询执行相反的操作,它首先按“部分”分组,然后按“类别”分组,然后按“用户名”分组。
请检查以下示例:
POST some_index/_bulk
{ "index" : { "_index" : "some_index", "_id" : "1" } }
{ "section" : "section1", "category" : "category1", "username" : "username1" }
{ "index" : { "_index" : "some_index", "_id" : "2" } }
{ "section" : "section2", "category" : "category1", "username" : "username1" }
{ "index" : { "_index" : "some_index", "_id" : "3" } }
{ "section" : "section1", "category" : "category2", "username" : "username2" }
{ "index" : { "_index" : "some_index", "_id" : "4" } }
{ "section" : "section2", "category" : "category2", "username" : "username2" }
GET some_index/_search
{
"size": 0,
"aggs": {
"top_category_username": {
"terms": {
"script": {
"source": "doc['category.keyword'].value + ' ' + doc['username.keyword'].value",
"lang": "painless"
},
"size": 20,
"order": {
"_count": "desc"
}
},
"aggs": {
"top_section": {
"terms": {
"field": "section.keyword",
"size": 1,
"order": {
"_count": "desc"
}
}
}
}
}
}
}
GET some_index/_search
{
"size": 0,
"aggs": {
"sections": {
"terms": {
"field": "section.keyword",
"size": 10
},
"aggs": {
"category_user_combinations": {
"terms": {
"script": {
"source": "doc['category.keyword'].value + ' ' + doc['username.keyword'].value",
"lang": "painless"
},
"size": 20,
"order": {
"_count": "desc"
}
}
}
}
}
}
}