java - Spring Boot Elasticsearch 在服务器启动时失败

标签 java spring-boot elasticsearch

我们有一个spring boot应用程序,其中我们使用spring数据 flex 搜索。我们使用实体类来生成 flex 索引。下面显示了一个这样的类-


import javax.persistence.Id;

import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;


/**
 * The Class ElasticSearchSampleEntity.
 */
@Document(indexName = "Sample-index", type = "Sample-content")
public class ElasticSearchSampleEntity {

    /** The es Sample entity id. */
    @Id
    @Field(type = FieldType.Long, index = true, name = "id")
    private Long esSampleEntityId;


    /** The es no of questions. */
    @Field(type = FieldType.Integer, index = true)
    private Integer esNoOfQuestions;

    /** The es total marks. */
    @Field(type = FieldType.Integer, index = true)
    private Integer esTotalMarks;

    /** The es total time min. */
    @Field(type = FieldType.Short, index = true)
    private Short esTotalTimeMin;

    /** The es total time sec. */
    @Field(type = FieldType.Short, index = true)
    private Short esTotalTimeSec;

    /** The es occurances. */
    @Field(type = FieldType.Integer, index = true)
    private Integer esOccurances;


    /** The es rating. */
    @Field(type = FieldType.Float, index = true)
    private Float esRating;


    /** The es created on. */
    @Field(type = FieldType.Long, index = true)
    private Long esCreatedDate;

    /** The es updated on. */
    @Field(type = FieldType.Long, index = true)
    private Long esUpdatedDate;

    /** The es published date. */
    @Field(type = FieldType.Long, index = true)
    private Long publishedDate;

    /** The sample type id. */
    @Field(type = FieldType.Integer, index = true)
    private Integer sampleTypeId;


    //getter and setter

}
这应该一次创建索引并将数据存储到 flex 服务器。但是,在部署期间,我们有时会遇到以下错误(间歇性问题)。
[main] ERROR o.s.boot.SpringApplication.reportFailure - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sampleController': Unsatisfied dependency expressed through field 'esSampleService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticSearchSampleServiceImpl': Unsatisfied dependency expressed through field 'esSampleRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticSearchSampleRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: mapper [sampleTypeId] cannot be changed from type [long] to [integer]
SampleTypeId有时会被创建为(或更新为)Long,即使我们专门将其称为Integer。这就是导致问题的原因。我在保存到 flex 数据库的同时检查了数据类型,这看起来还不错。这是否意味着 flex 索引会随着每次部署进行更新。对于数据库,我们对 flex 类似地执行spring.jpa.hibernate.ddl-auto=validate,我们也可以这样做吗?我无法理解为什么会发生此问题。映射似乎很好。请在这里帮助我。

最佳答案

错误日志中的以下片段很重要。

Constructor threw exception; nested exception is java.lang.IllegalArgumentException: mapper [sampleTypeId] cannot be changed from type [long] to [integer]


看起来像之前创建索引时,您的sampleTypeId被定义为long,现在您将其更改为int,这是不可能的,因此从elasticsearch抛出异常。
您可以从elasticsearch GET index mapping API进行确认,并检查索引中sampleTypeId的数据类型。

关于java - Spring Boot Elasticsearch 在服务器启动时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64695129/

相关文章:

java - 如何使用 REST API 将子对象添加到父对象?

java - 如何减少 Elasticsearch 滚动响应时间?

elasticsearch - 如何使Elasticsearch聚合仅创建1个存储桶?

angularjs - Stormpath 是否允许 CORS 支持 Springboot + AngularJS 架构?

java - 找不到类型为 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' 的 bean

elasticsearch - 根据查询词对Elasticsearch查询返回的结果进行分组

java - Android 货币格式返回不同的货币符号

java - s.select 标签呈现空下拉列表

java - 我无法从 plpgsql 函数中读取 java 返回的数组

java - 在 selenium web 驱动程序中迭代具有相同名称的元素时索引超出范围异常