一、问题描述
版本:
ES:6.8.3
使用spring-data-elasticsearch
操作es增删改查,进行排序操作的时候出现了一下报错:
Caused by: org.elasticsearch.ElasticsearchException:
Elasticsearch exception [type=illegal_argument_exception, reason=Fielddata is disabled on text fields by default.
Set fielddata=true on [receive_time] 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.]
二、解决方法
出现这个报错的原因是由于我在查询后对text类型的字段进行了排序操作。
通过GET /索引名/_mapping
查看索引的mapping映射,发现该排序的字段类型是text
类型,而text类型的字段是不能进行排序和聚合操作的。
修改后:
//设置分页和根据创建时间降序排序 注意:page从0开始
//text字段不能用于排序和聚合 如果是text字段,则需要receive_time.keyword进行排序
PageRequest page = PageRequest.of
(pageNum-1, pageSize, Sort.by(Sort.Order.desc("receive_time.keyword")));
Page<WarnData> data = esWarnDataRepository.search(base_query, page);
解决。