【问题标题】:Spring boot elastic search fails on server start up服务器启动时 Spring Boot 弹性搜索失败
【发布时间】:2021-02-18 00:52:46
【问题描述】:

我们有一个使用 Spring Data 弹性搜索的 Spring Boot 应用程序。我们使用实体类来生成弹性索引。一个这样的类如下所示 -


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

}

这应该创建一次索引并将数据存储到弹性服务器。但是,在部署过程中,我们有时会遇到以下错误(间歇性问题)。

[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。这就是导致问题的原因。我在保存到弹性数据库时检查了数据类型,看起来还不错。这是否意味着弹性索引会随着每次部署而更新。对于数据库我们做spring.jpa.hibernate.ddl-auto=validate 同样对于弹性我们可以做同样的事情吗?我无法理解为什么会发生这个问题。映射似乎很好。请帮帮我。

【问题讨论】:

    标签: java spring-boot elasticsearch


    【解决方案1】:

    错误日志中的以下 sn-p 很重要。

    构造函数抛出异常;嵌套异常是 java.lang.IllegalArgumentException:映射器 [sampleTypeId] 不能 从类型 [long] 更改为 [integer]

    看起来您之前创建了将sampleTypeId 定义为long 的索引,现在您将其更改为int,这是不可能的,因此从elasticsearch 引发了异常。

    您可以从您的elasticsearch GET index mapping API 中确认,并在索引中检查您的sampleTypeId 的数据类型。

    【讨论】:

    • 是的,我已经检查了 get API。我明白这就是问题所在。但是在我的实体类中,如问题所示,它被称为整数。我不明白它在何时何地(以及如何)更改为 Long。 SampleType Id 被定义为 Integer,它有时会变长。在我的某些部署中,同样的事情也适用。这让我很困惑。
    • @TheNightsWatch 这很奇怪,这个字段的映射是否有任何其他方式正在发生变化?我建议使用静态映射来避免任何此类问题,并尽可能禁用映射更新。
    • 感谢您的宝贵时间。我还不知道它在哪里发生了变化以及如何关闭对映射的更新。
    • @TheNightsWatch 你检查了 ES 索引日志来解决这个问题,无论如何我可以告诉你问题是什么以及如何解决它,但你需要从日志中找出它:)
    • @TheNightsWatch,希望你是安全的,如果你能在这里提供更新会很棒:)
    猜你喜欢
    • 1970-01-01
    • 2018-06-08
    • 2014-11-04
    • 1970-01-01
    • 2019-07-18
    • 2015-03-28
    • 2020-10-22
    • 2016-06-15
    • 2021-10-25
    相关资源
    最近更新 更多