分类
大数据

ES聚合操作 – Fielddata is disabled on text fields by default

使用ES聚合操作对字段(projectCode)去重:

GET bury-point-click/_search
{
  "size": 0,
  "aggs": {
    "group_by_project": {
      "cardinality": {
        "field": "projectCode"
      }
    }
  }
}

执行报错:Fielddata is disabled on text fields by default. Set fielddata=true on [projectCode] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.

这是因为ES对排序,聚合这些操作用单独的数据结构(fielddata)缓存到内存里,Text字段数据会占用大量堆空间,需要单独开启。

开启代码如下:

PUT bury-point-click/_mapping/
{
  "properties": {
    "projectCode": { 
      "type":     "text",
      "fielddata": true
    }
  }
}

再执行原来的语句,正常返回结果。

参考

https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

“ES聚合操作 – Fielddata is disabled on text fields by default”上的一条回复

在es 5.3 以后,去重可以使用 Collapsing 字段折叠。在查询关键字加上keyword也可以实现聚合操作。

GET bury-point-click/_search/
{
"size": 0,
"aggs": {
"group_by_project": {
"terms": {
"field": "projectCode.keyword"
}
}
}
}

发表评论

电子邮件地址不会被公开。