服务器启动时,Spring引导弹性搜索失败



我们有一个spring-boot应用程序,在其中我们使用spring-data弹性搜索。我们使用实体类来生成弹性索引。一个这样的类别如下所示-


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.lang.IollegalArgumentException:映射程序[sampleTypeId]不能为从类型[long]更改为[integer]

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

您可以从elasticsearch GET索引映射API中确认这一点,并检查索引中sampleTypeId的数据类型

最新更新