spring-boot - 文本字段未针对需要每个文档 : Elasticsearch 的操作进行优化

标签 spring-boot elasticsearch spring-data-elasticsearch elasticsearch-aggregation

这是我的实体:

public class RecentTransactionBo {

    @JsonProperty("timestamp")
    @Field(type = FieldType.Date, format = DateFormat.basic_date_time)
    @Id
    @Temporal(TemporalType.DATE)
    private Date creationDate;
    private List<String> transactionId;

}

我正在对其进行排序操作:

Iterable<RecentTransactionBo> recentTransactionBoIterable = recentTransactionDao.findAll(Sort.by(Sort.Direction.DESC, "creationDate"));

我收到以下错误:

2020-09-08 02:42:29.116 ERROR 7116 --- [nio-8090-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.elasticsearch.UncategorizedElasticsearchException: Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]; nested: ElasticsearchException[Elasticsearch exception [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 [creationDate] in order to load field data by uninverting the inverted index. Note that this can use significant memory.]]; nested: ElasticsearchException[Elasticsearch exception [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 [creationDate] in order to load field data by uninverting the inverted index. Note that this can use significant memory.]];] with root cause

    org.elasticsearch.ElasticsearchException: Elasticsearch exception [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 [creationDate] in order to load field data by uninverting the inverted index. Note that this can use significant memory.]

我的 DAO 看起来像这样。

@Repository
public interface RecentTransactionDao extends ElasticsearchRepository<RecentTransactionBo, Date> {

}

我该如何解决这个问题,我犯了什么错误? 我使用的是 7.8.1 版本

最佳答案

要正确解决此问题,您应该创建正确的映射并将必填字段设置为 keyword 类型。

其他解决方案是运行命令:

PUT <your_index_name>/_mapping
{
  "properties": {
    "creationDate": { 
      "type":     "text",
      "fielddata": true
    },
    "host.name": { 
      "type":     "text",
      "fielddata": true
    }
  }
}

上面我在 2 个字段上设置 fielddata 属性 - creationDatehost.name 作为如何在多个字段上设置它的示例在一次通话中。 Elastic 一次只会给你一个名字,所以你可能需要多次运行命令,除非你事先知道所有有问题的字段。

关于spring-boot - 文本字段未针对需要每个文档 : Elasticsearch 的操作进行优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63784500/

相关文章:

java - Adwords 库中的枚举成员为 null

java - 我可以在 Synology NAS 中部署 Java Spring-Boot 应用程序吗?

java - Keycloak:调用管理员 rest api 时无法将用户领域角色读取为管理员 cli

elasticsearch - 查找字段不存在或字段小于值的文档

spring-boot - 当SpringBoot应用程序请求ElasticSearch时,是否可以将所有索引数据搜索上的Fuzzy参数设置为应用程序参数?

java - 尝试使用 ssh 私钥文件设置 Spring Boot 云配置服务器并获取 : ucom. jcraft.jsch.JSchException:USERAUTH 失败

elasticsearch - ElasticSearch中唯一嵌套文档的数量

elasticsearch - 如何基于kubernetes元数据创建索引

Spring 数据 Elasticsearch : Can't merge because of conflicts: [Cannot update enabled setting for [_source]]

spring - 如何在没有kibana的情况下为Elasticsearch运行感知插件?