【发布时间】:2020-02-20 04:22:59
【问题描述】:
elasticsearch 中的聚合新手。使用 7.2。我正在尝试在 Tree.keyword 上编写一个聚合,以仅返回具有包含单词“Branch”的键的文档的计数。我尝试过子聚合、bucket_selector(不适用于键字符串)和脚本。有人对如何解决这个问题有任何想法或建议吗?
映射:
{
"testindex" : {
"mappings" : {
"properties" : {
"Tree" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
}
}
}
}
}
返回所有键的示例查询,但我需要做的是限制只返回带有“分支”或更好的键,但只返回有多少“分支”键的计数:
GET testindex/_search
{
"aggs": {
"bucket": {
"terms": {
"field": "Tree.keyword"
}
}
}
}
返回:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"Tree" : [
"Car:76",
"Branch:yellow",
"Car:one",
"Branch:blue"
]
}
}
]
},
"aggregations" : {
"bucket" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Car:76",
"doc_count" : 1
},
{
"key" : "Branch:yellow",
"doc_count" : 1
},
{
"key" : "Car:one",
"doc_count" : 1
},
{
"key" : "Branch:blue",
"doc_count" : 1
}
]
}
}
}
【问题讨论】:
-
你应该看看下面的答案:stackoverflow.com/a/45982797/7674214
标签: elasticsearch elasticsearch-aggregation