关于聚合:Elasticsearch-实现-sql-count-distinct-的功能qbit

41次阅读

共计 568 个字符,预计需要花费 2 分钟才能阅读完成。

前言

  • 本文对 Elasticsearch 7.17 实用

count distinct

  • 单字段

    "aggs": {
      "card": {
        "cardinality": {"field": "type"}
      }
    }
  • 多字段

    "aggs": {
      "multi_field_cardinality": {
        "cardinality": {"script": "doc['type'].value +'#'+ doc['color'].value"
        }
      }
    }

    OR

    // 这个写法比下面的写法效率高很多
    {
    "runtime_mappings": {
      "type_and_color": {
        "type": "keyword",
        "script": "emit(doc['type'].value +'#'+ doc['color'].value)"
      }
    },
    "aggs": {
      "type_and_color": {
        "cardinality": {"field": "type_and_color"}
      }
    }
    }

相干浏览

  • Elasticsearch Cardinality aggregation 官网文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/…
  • StackOverflow:Getting cardinality of multiple fields?

本文出自 qbit snap

正文完
 0