使用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
发表回复