因此,在 Elasticsearch 中,我可以执行以下操作:
{
"aggs": {
"title": {
"terms": {
"field": "title",
"shard_size": 50,
"size": 5
}
}
},
"query": {...},
"size": 0
}
这将返回前5个标题的文档计数,因此我们得到的结果(部分)如下:
"buckets" : [
{
"key" : "Delivery Driver",
"doc_count" : 1495
},
{
"key" : "Assistant Manager",
"doc_count" : 1250
},
{
"key" : "Server",
"doc_count" : 1175
},
{
"key" : "Dishwasher",
"doc_count" : 966
},
{
"key" : "Team Member",
"doc_count" : 960
}
]
但是现在我需要将文档计数到一些自定义存储桶中,因此我需要执行以下操作:
{
"aggs": {
"loc": {
"filters": {
"filters": {
"1042_2": {
"terms": {
"counties": [
...
]
}
},
"1594_2": {
"terms": {
"counties": [
...
]
}
},
"1714_2": {
"terms": {
"counties": [
...
]
}
},
"1746_2": {
"terms": {
"counties": [
...
]
}
},
"1814_2": {
"terms": {
"counties": [
...
]
}
},
"1943_2": {
"terms": {
"counties": [
...
]
}
},
"2658_2": {
"terms": {
"counties": [
...
]
}
}
}
}
}
},
"query": {...},
"size": 0
}
请注意,有7个存储分区,因为我们不知道哪个存储分区最大。运行此命令将返回我们:
"buckets" : {
"1042_2" : {
"doc_count" : 23687
},
"1594_2" : {
"doc_count" : 8951
},
"1714_2" : {
"doc_count" : 52555
},
"1746_2" : {
"doc_count" : 60534
},
"1814_2" : {
"doc_count" : 63956
},
"1943_2" : {
"doc_count" : 25533
},
"2658_2" : {
"doc_count" : 534
}
}
但是我希望只给我最大的5个而不是所有的存储桶。有没有办法像
size
下的terms
参数那样将其限制为仅n个最大存储桶?
最佳答案
size
参数对于过滤器聚合没有意义,因为通过指定过滤器,您已经明确指定/控制要创建和返回的存储桶数。
不过,您可能要考虑的是,创建了所有潜在的存储桶,然后使用order
-clause按降序对它们进行排序。
在客户端,您只需“消费”第一个n
存储桶。
关于elasticsearch - 仅返回前N个桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59398693/