Fielddata is disabled on text fields by default
Fielddata is disabled on text fields by default
从 elasticsearch 中查询数据时,指定了按 created_at 字段排序,遇到:
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] 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 中 created_at 的字段属性:
{
"created_at":{
"type": "text",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above": 256
}
}
}
}
第一种处理方式:(参考 es 官方文档:Use the my_field.keyword field for aggregations, sorting, or in scripts.),查询时,在字段后面加上 keyword,组装后发给 es 的查询条件:
"sort": {
"created_at.keyword": {
"order": "desc"
}
}
第二种处理方式:(参考 es 官方文档),在 es 中的 created_at 字段上设置 fielddata
为 true:
# 官方文档中的示例:
PUT my-index-000001/_mapping
{
"properties": {
"my_field": {
"type": "text",
"fielddata": true
}
}
}
# 实际执行
PUT log/_mapping/doc/
{
"properties": {
"created_at": {
"type": "text",
"fielddata": true
}
}
}
# 执行结果
{
"acknowledged": true
}