背景说明
最近在做一个 Elasticsearch 的分页查询,并且对查询结果按照特定字段进行排序的功能。
但是执行结果却报错,报错信息如下:
{ "error": { "root_cause": [ { "type": "illegal_argument_exception", "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [state] 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": "alarm", "node": "hdLJanxRTbmF52eK6-FFgg", "reason": { "type": "illegal_argument_exception", "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [state] 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 }
原因分析
查询语句如下:
GET alarm/_search // index为 alarm { "query" : { "bool" : { "must" : [ { "match_phrase" : { "state" : { "query" : "confirmed", "slop" : 0, "boost" : 1.0 } } } ], "disable_coord" : false, "adjust_pure_negative" : true, "boost" : 1.0 } }, "from": 1, // 分页,第几页开始 "size": 5, // 分页,每页显示多少条 "sort": { // 排序,按照 state 字段降序排序 "state": { "order": "desc" } } }