淘先锋技术网

首页 1 2 3 4 5 6 7

我们都知道cardinality 默认情况下只能传入一个字段,用来计算这一个字段的唯一值
形如下

PUT ellisintstring/
{
  "mappings": {
    "properties": {
      "ceshi":{
        "type": "nested"
      }
    }
  }
}

PUT ellisintstring/_doc/1
{
  "ceshi":{
    "item":"0001",
    "shuju":"haha"
  }
}

PUT ellisintstring/_doc/2-10
{
  "ceshi":{
    "item":"0401",
    "shuju":"haha"
  }
}

PUT ellisintstring/_doc/11
{
  "ceshi":{
    "item":"0014",
    "shuju":"haha5"
  }
}

字段唯一值查询

GET ellisintstring/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "haha": {
      "nested": {
        "path": "ceshi"
      },"aggs": {
        "distinct": {
          "cardinality": {
            "field": "ceshi.item.keyword"
          }
        }
      }
    }
  }
}

现在想多字段联合唯一

GET ellisintstring/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "haha": {
      "nested": {
        "path": "ceshi"
      },"aggs": {
        "distinct": {
          "cardinality": {
            "script": {
              "inline": "doc['ceshi.item.keyword'].value+doc['ceshi.shuju.keyword'].value"
            }
          }
        }
      }
    }
  }
}

针对item 以及shuju字段做两个runtime 字段,根据这两个字段求唯一值

GET ellisintstring/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "haha": {
      "nested": {
        "path": "ceshi"
      },"aggs": {
        "distinct": {
          "cardinality": {
            "script": {
              "inline": "doc['ii'].value+doc['ss'].value"
            }
          }
        }
      }
    }
  },
  "runtime_mappings":{
    "ii":{
      "type":"keyword",
      "script":{
        "source":"""
        emit(doc["ceshi.item.keyword"].value.substring(0, 1))
        """
      }
    },
    "ss":{
      "type":"keyword",
      "script":{
        "source":"""
        emit(doc["ceshi.shuju.keyword"].value.substring(0, 1))
        """
      }
    }
    }
}

https://stackoverflow.com/questions/47458352/filter-elasticsearch-aggregation-by-bucket-key-value

https://discuss.elastic.co/t/cardinality-aggregation-on-several-fields/34519/2

https://github.com/elastic/elasticsearch/issues/87072