首页 > 解决方案 > elasticsearch 中的 sql count(dictinct())) 等价物是什么?

问题描述

我想问一下 elasticsearch 中的 sql count(dictinct())) 是什么?我的意思是如何使以下 sql 查询成为 elasticsearch 查询。我尝试了一些聚合,但我没有找到确切的答案。

select count(distinct(unique_field)) where db_date < "..." and db_date > "..."

我已经尝试了下面的代码,但它没有通过 sql 给出预期的结果。

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "city": {
              "value": "bratislava"
            }
          }
        },
        {
          "range": {
            "db_date": {
              "gte": "2020-05-15T00:00:00.000Z",
              "lte": "2020-05-16T00:00:00.000Z"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "types_count": {
      "value_count": {
        "field": "unique_field"
      }
    },
    "group_by_status": {
      "terms": {
        "field": "unique_field"
      }
    }
  }
}

谢谢回答

标签: elasticsearch

解决方案


推荐阅读