elasticsearch cardinality 多字段联合唯一,以及如何访问runtime_fields

我们都知道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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值