gpt4 book ai didi

elasticsearch - 如何根据单个字段中的值和每个存储桶的计数获得多个字母聚合

转载 作者:行者123 更新时间:2023-12-03 02:28:54 25 4
gpt4 key购买 nike

我有一个字段:name.keyword。
我希望聚合(或其中的一组以分层分解的形式排列)结果如下所示:

all_names - a count of all distinct values for the name.keyword field
names_a_through_e - a count of all distinct values that are limited to those values that start with "A" through those that start with "E"
names_a - a count of all distinct values that are limited to those that start with "A"
names - a list of each of these names and their count
names_b - a count of all distinct values that are limited to those that start with "B"
names - a list of each of these names and their count
...
names_f_through_j - a count of all distinct values that are limited to those values that start with "F" through those that start with "J"
names_f - a count of all distinct values that are limited to those that start with "F"
names - a list of each of these names and their count
names_g - a count of all distinct values that are limited to those that start with "G"
names - a list of each of these names and their count
...
...

我当然可以像这样涵盖最内部的列表:
    "a_names": {
"terms": {
"field": "name.keyword",
"include": "A.*",
"size": 100,
"order": {"_term": "asc"}
}
}

但这并没有给我这个级别的计数 - 所有在 name.keyword 字段中带有 A* 的文档的总和。

或者有没有办法在单个字段上进行这种结构化的分桶,可以像我描述的那样很好地组织布局?

最佳答案

您可以使用sum_bucket aggregation计算存储桶中的总文档数。

询问

 {
"size": 0,
"aggs": {
"a_names": {
"terms": {
"field": "name.keyword",
"include": "A.*",
"size": 100,
"order": {
"_term": "asc"
}
}
},
"totalcount": {
"sum_bucket": {
"buckets_path": "a_names._count"
}
}
}
}

结果:
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"a_names" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Abc",
"doc_count" : 1,
"Count" : {
"value" : 1
}
},
{
"key" : "Acf",
"doc_count" : 1,
"Count" : {
"value" : 1
}
}
]
},
"totalcount" : {
"value" : 2.0
}
}

关于elasticsearch - 如何根据单个字段中的值和每个存储桶的计数获得多个字母聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60436883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com