uniq性别只返回10个值.而我需要所有独特的价值观



问题陈述:我需要完整索引中metrichost.name.keyword的唯一值列表。目前,我使用的是下面的查询,它只给出10个值,但索引中还有更多的值。

查询:

GET nw-metricbeats-7.10.0-2021.07.16/_search
{
"size":"0",
"aggs" :
{
"uniq_gender" : 
{
"terms" : 
{ 
"field" : "host.name.keyword" 

}
}
}
}

目前,它只返回10个值,如下所示:

{
"took" : 68,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"uniq_gender" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 1011615,
"buckets" : [
{
"key" : "service1",
"doc_count" : 303710
},
{
"key" : "service2",
"doc_count" : 155110
},
{
"key" : "service3",
"doc_count" : 154074
},
{
"key" : "service4",
"doc_count" : 148499
},
{
"key" : "service5",
"doc_count" : 145033
},
{
"key" : "service6",
"doc_count" : 144226
},
{
"key" : "service7",
"doc_count" : 139367
},
{
"key" : "service8",
"doc_count" : 137063
},
{
"key" : "service9",
"doc_count" : 135586
},
{
"key" : "service10",
"doc_count" : 134794
}
]
}
}
}

有人能帮我查询可以从度量中返回N个唯一值吗??

您有两个选项。如果您对字段将采用的值的数量有一点了解,则可以传递一个大于该数字的size参数。

{
"size":"0",
"aggs" :
{
"uniq_gender" : 
{
"terms" : 
{ 
"field" : "host.name.keyword",
"size" : 500 
}
}
}
}

这可能不是你的最佳解决方案,因为:

1:您必须传入一个固定的大小值。2:因为结果可能不是完全准确的

Elasticsearch文档建议使用复合聚合作为一种替代方案。

{
"size": 0,
"aggs": {
"my_buckets": {
"composite": {
"sources": [
{ "uniq_gender": { "terms": { "field": "host.name.keyword" } } }
]
}
}
}
}

您的terms-agg还接受一个size参数,该参数设置要返回的bucket数量。默认值为10。

我要提醒您不要依赖这种方法来查找任何具有非常高基数的字段的所有索引值,因为这是一种臭名昭著的破坏节点堆使用的方法。为此提供了一个复合agg。

最新更新