【问题标题】:can i use count query when distinct a field in Elasticsearch?我可以在 Elasticsearch 中区分一个字段时使用计数查询吗?
【发布时间】:2021-08-31 10:02:59
【问题描述】:

在 Elasticsearch 中使用不同的 a 字段时如何获取元素计数?我想在不同的字段之一时获得索引的总元素。我可以使用这些代码进行搜索:

**POST myIndex/_search**
{
  "size": 0,
  "aggs": {
    "myField": {
      "terms": {
        "field": "name’s of my field",
        "size": 10000
      }
    }
  }
.
.
.
}

但是,我想查询类似于:

**GET myIndex/_count**
{
 "size": 0,
  "aggs": {
    "myField": {
      "terms": {
        "field": "name’s of my field",
        "size": 10000
      }
    }
  }
.
.
.
}

但返回错误:

**{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "request does not support [size]",
        "line" : 2,
        "col" : 3
      }
    ],
    "type" : "parsing_exception",
    "reason" : "request does not support [size]",
    "line" : 2,
    "col" : 3
  },
  "status" : 400
}**

所以我对这个问题的解决方案感兴趣。

【问题讨论】:

    标签: elasticsearch count distinct


    【解决方案1】:

    弹性搜索只支持使用cardinality aggregation的近似Distinct

    {
      "aggs": {
        "distinct_count": {
          "cardinality": {
            "field": "field-name"
          }
        }
      }
    }
    

    值是近似值。虽然您可以使用precision_threshold 提高精度

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多