在觀看ES文檔時做了一些基礎(chǔ)測試,在執(zhí)行以下操作時出現(xiàn)報錯
GET /megacorp/employee/_search
{
"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": "3DTOZs6yRsS8kxERdFJoHw",
"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."
}
}
],
"caused_by": {
"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.",
"caused_by": {
"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
}
經(jīng)過查找驗證后發(fā)現(xiàn)出現(xiàn)該錯誤是因為5.x之后
泽台,Elasticsearch對排序寻狂、聚合所依據(jù)的字段用單獨的數(shù)據(jù)結(jié)構(gòu)(fielddata)緩存到內(nèi)存
里了蠕嫁,但是在text字段上默認是禁用的,如果有需要單獨開啟菊卷,這樣做的目的是為了節(jié)省內(nèi)存空間缔恳。
所以如果需要進行聚合操作,需要單獨開啟洁闰。
執(zhí)行以下代碼:
PUT megacorp/_mapping/employee/
{
"properties": {
"interests": {
"type": "text",
"fielddata": true
}
}
}
設(shè)置成功后會出現(xiàn)如下結(jié)果:
{
"acknowledged" : true
}
再次執(zhí)行查詢后就出現(xiàn)想要的結(jié)果了歉甚。
轉(zhuǎn)自:https://blog.csdn.net/abandon_li/article/details/87285887