gpt4 book ai didi

elasticsearch - 根据文档总数过滤掉 top_hits 子聚合

转载 作者:行者123 更新时间:2023-11-29 02:48:12 27 4
gpt4 key购买 nike

我正在使用 Elasticsearch GeoHash 网格聚合进行 map 聚类。查询平均返回 100-200 个桶。每个桶都使用 top_hits 聚合,我用它为每个聚合集群返回 3 个文档。

问题是我想返回 top_hits 仅当父聚合 (GeoHash) 聚合不超过 3 个文档时。

如果一个集群聚合了超过 3 个文档,我不希望 ES 为这个集群返回任何文档(因为我不会使用它们)。

我试过使用 Bucket Selector Aggregation ,但未能构建正确的bucket_path。我在与 top_hits 聚合相同的级别上使用桶选择器聚合。top_hits.hits.total 提供了存储桶的总文档数,但我得到的是 reason=path not supported for [top_hits]: [hits.total].

这在 elasticsearch 中可行吗?这对我很重要,因为在大多数查询中,只有一小部分存储桶的文档少于 3 个。但是即使对于 1000 个文档的集群,top hits subaggregation 总是返回前 3 个文档。如果查询结果返回 200 个存储桶,并且其中只有 5 个正在聚合 <= 3 个文档,那么我只想返回 5*3 个文档,而不是 200*3 个(在这种情况下,响应是 10MB)。

这是我查询的聚合部分:

"clusters": {
"geohash_grid": {
"field": "coordinates",
"precision": 3
},
"aggs": {
"top_hits": {
"top_hits": {
"size": 3
}
},
"top_hits_filter": {
"bucket_selector": {
"buckets_path": {
"total_hits": "top_hits._count" // tried top_hits.hits.total
},
"script": {
"inline": "total_hits <= 3"
}
}
}
}
}

最佳答案

试试这个@ilivewithian:

  "aggs": {
"clusters": {
"geohash_grid": {
"field": "coordinates",
"precision": 3
},
"aggs": {
"top_hits": {
"top_hits": {
"size": 3
}
},
"top_hits_filter": {
"bucket_selector": {
"buckets_path": {
"total_hits": "_count"
},
"script": {
"inline": "params.total_hits <= 3"
}
}
}
}
}
}

关于elasticsearch - 根据文档总数过滤掉 top_hits 子聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35938540/

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