【发布时间】:2021-05-06 04:23:41
【问题描述】:
我遇到了一个问题,因为我需要使用关键字字段对存储桶进行排序,为此我尝试了两种方法。
- 我一直在尝试从热门聚合中对聚合(存储桶)的结果进行排序。我的 top_hits 包含一个元素,即用户名
"user_data": {
"top_hits": {
"_source": {
"includes": ["username"]
},
"size": 1
}
},
为了对我正在尝试使用存储桶排序的存储桶进行排序,存储桶排序是这样的
sorting": {
"bucket_sort": {
"sort": [
{
"user_data>username": { ----> This is the error
"order": "desc"
}
}
],
"from": 0,
"size": 25
}
}
但是我收到了一个语法错误,基本上存储桶路径是错误的。
- 我用来完成排序的另一种方法是在用户名上添加另一个聚合以获得最大值。像这样的
"to_sort" : {
"max": {
"field": "username"
}
}
并使用下面的bucket_sort
"sorting": {
"bucket_sort": {
"sort": [
{
"to_sort": {
"order": "desc"
}
}
],
"from": 0,
"size": 25
}
}
但基本上我不能使用关键字字段来使用最大聚合。 有没有办法使用用户名对我的存储桶进行排序,用户名是关键字字段?
我的聚合的父级是
"aggs": {
"CountryId": {
"terms": {
"field": "countryId",
"size": 10000
}
每个bucket的用户名值不同
桶的结果是这样的
"buckets" : [
{
"key" : "11111",
"doc_count" : 17,
"user_data" : {
"hits" : {
"total" : 10,
"max_score" : 11,
"hits" : [
{
"_index" : "index_name",
"_type" : "index_name",
"_id" : "101010",
"_score" : 0.0,
"_source" : {
"username" : "cccccc"
}
}
]
}
}
},
{
"key" : "33333",
"doc_count" : 17,
"user_data" : {
"hits" : {
"total" : 10,
"max_score" : 11,
"hits" : [
{
"_index" : "index_name",
"_type" : "index_name",
"_id" : "101010",
"_score" : 0.0,
"_source" : {
"username" : "bbbbb"
}
}
]
}
}
},
{
"key" : "22222",
"doc_count" : 17,
"user_data" : {
"hits" : {
"total" : 10,
"max_score" : 11,
"hits" : [
{
"_index" : "index_name",
"_type" : "index_name",
"_id" : "101010",
"_score" : 0.0,
"_source" : {
"username" : "aaaaa"
}
}
]
}
}
}
]
我想要以下桶结果
"buckets" : [
{
"key" : "22222",
"doc_count" : 17,
"user_data" : {
"hits" : {
"total" : 10,
"max_score" : 11,
"hits" : [
{
"_index" : "index_name",
"_type" : "index_name",
"_id" : "101010",
"_score" : 0.0,
"_source" : {
"username" : "aaaaa"
}
}
]
}
}
},
{
"key" : "33333",
"doc_count" : 17,
"user_data" : {
"hits" : {
"total" : 10,
"max_score" : 11,
"hits" : [
{
"_index" : "index_name",
"_type" : "index_name",
"_id" : "101010",
"_score" : 0.0,
"_source" : {
"username" : "bbbbb"
}
}
]
}
}
},
{
"key" : "11111",
"doc_count" : 17,
"user_data" : {
"hits" : {
"total" : 10,
"max_score" : 11,
"hits" : [
{
"_index" : "index_name",
"_type" : "index_name",
"_id" : "101010",
"_score" : 0.0,
"_source" : {
"username" : "ccccc"
}
}
]
}
}
}
]
如何查看按用户名排序的存储桶。
【问题讨论】:
-
你能说明什么是父聚合以及它是在哪个字段上计算的吗?每个桶还有一个唯一的
username值吗? -
我添加了有关我的问题的更多详细信息。
标签: elasticsearch kibana elasticsearch-aggregation