gpt4 book ai didi

elasticsearch - 限制 Elasticsearch 中的分组结果

转载 作者:行者123 更新时间:2023-11-29 02:54:41 25 4
gpt4 key购买 nike

在我的 elasticsearch 索引“people”中有以下文档:

{"name": "John", "district": 1},
{"name": "Anne", "district": 1},
{"name": "Mary", "district": 2},
{"name": "Bobby", "district": 2},
{"name": "Nick", "district": 1},
{"name": "Bob", "district": 3},
{"name": "Kenny", "district": 1}

我想得到分区为 2 或 1 的文档的结果,但最多只有 2 个。因此,如果以上是我的整个索引,我希望它返回:

{"name": "John", "district": 1},
{"name": "Anne", "district": 1},
{"name": "Mary", "district": 2},
{"name": "Bobby", "district": 2},

是否可以通过弹性中的单个查询来实现?非常感谢您的帮助!

最佳答案

应该这样做:

GET /some_index/some_type/_search?search_type=count
{
"aggs": {
"district_1_or_2": {
"filter": {
"bool": {
"should": [
{
"term": {
"district": 1
}
},
{
"term": {
"district": 2
}
}
]
}
},
"aggs": {
"district": {
"terms": {
"field": "district",
"size": 10
},
"aggs": {
"top": {
"top_hits": {
"size": 2
}
}
}
}
}
}
}
}

关于elasticsearch - 限制 Elasticsearch 中的分组结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31795599/

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