初次学习 ​​Elasticsearch​​ 在参照文档实例 ​​挖掘出员工中最受欢迎的兴趣爱好​​ 这一章节时,执行命令

curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}'


出现如下的错误信息

{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [interests] 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."
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "megacorp",
"node": "8UDhP_KCSz6CNmKNyyFriQ",
"reason": {
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [interests] 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."
}
}
]
},
"status": 400
}


具体原因是聚合查询需要大量的内存,去和前,需要将相应的字段开启聚合。

curl -X PUT "localhost:9200/megacorp/_mapping/employee" -H 'Content-Type: application/json' -d'{
"properties": {
"interests": {
"type": "text",
"fielddata": true
}
}
}'