【发布时间】: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