【问题标题】:Can I calculate the cardinality of the _id field in Elasticsearch?我可以计算 Elasticsearch 中 _id 字段的基数吗?
【发布时间】:2015-05-15 17:54:53
【问题描述】:

我有多个 Elasticsearch 1.3.2 索引,并且我正在使用自定义文档 ID。我想在我的索引中找到不同 ID 的数量。有些文档具有相同的 ID,但在不同的索引中,因此这与仅计算文档不同。所以我想对 _id 字段进行基数聚合。所以我把这个发到http://localhost:9200/*my_indices*/_search

{ "from": 0, "size": 0, "aggregations": { "_count": { "cardinality": { "script": "doc['_id'].value", "lang": "groovy" } } } }

但是 Elasticsearch 刚刚发回了这个:

{ "took": 60, "timed_out": false, "_shards": { "total": 175, "successful": 175, "failed": 0 }, "hits": { "total": 310714, "max_score": 0, "hits": [] }, "aggregations": { "_count": { "value": 0 } } 

我很确定那里有超过 0 个 ID!发生了什么,有没有可能得到我想要的?

【问题讨论】:

    标签: groovy elasticsearch


    【解决方案1】:

    还有另一种解决方案,无需重新索引所有内容,即改用 _uid 字段:

    {
      "from": 0,
      "size": 0,
      "aggregations": {
        "_count": {
          "cardinality": {
            "field": "_uid"
          }
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      _id 字段为 not analyzed and not stored, by default。而且我认为它也没有存储在_source 中。您不能按原样将其与聚合一起使用。

      对于您的索引,您需要对其进行更改,以便将 _id 编入索引:

        "_id": {
          "index": "not_analyzed"
        }
      

      【讨论】:

      • 酷,我很高兴这是可能的。有没有办法修改我现有的索引以使 _id 被索引?
      • @InquisitiveIdiot 你可以,但你必须重新索引:(
      • @TheWhiteRabbit 说了什么:-)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多