uniq gender returns 只有 10 个值。而我需要所有唯一值

the uniq gender returns only 10 values. whereas I need all the unique values

问题陈述:我需要完整索引中的指标 host.name.keyword 的唯一值列表。目前,我正在使用下面的查询,它只给出了 10 个值,但索引中存在更多的值。

查询:

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

目前,它 return 只有 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
        }
      ]
    }
  }
}

有人可以帮我查询 return 来自指标的 N 个唯一值吗??

您的条款 agg 还接受一个 size 参数,该参数设置要返回的桶数。默认值为 10。

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

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

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

这可能不是最适合您的解决方案,因为:

1: 你必须在尺寸中传入一个固定值。 2:因为结果可能不完全accurate

Elasticsearch 文档使用建议 composite aggregation 作为备选方案。

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