在elasticsearch中进行sort排序遇到的bug,bug描述如下:
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [time] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
}
.......
该问题知识记录暂时没有详细深入的了解原理,但是结局方法应该可行。
搜索语句如下:
GET test_record/_search
{
"query": {
"regexp": {
"word": ".*"
}
},
"sort": [
{
"time": {
"order": "desc"
}
}
]
}
错误修改如下:
将time换成time.keysord
通过网络发现出现此错误的可能不止sort一种,aggs也可能出现这种错误。
错误的原因是我们sort或者aggs的字段是text类型,而text类型是作为有个整体提供默认没有索引。
而使用keyword是有索引的,因此可以通过time.keyword来sort或者aggs