IllegalArgumentException

环境 spring-data-elasticsearch 3.1.6
java.lang.IllegalArgumentException: Mapper for [publishDate] conflicts with existing mapping in other types: [mapper [publishDate] has different [format] values

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'app_Main': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Mapper for [publishDate] conflicts with existing mapping in other types:
[mapper [publishDate] has different [format] values]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
...
...
...
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at fluffy.mo.App_Main.main(App_Main.java:13) [classes/:na]
Caused by: java.lang.IllegalArgumentException: Mapper for [publishDate3] conflicts with existing mapping in other types:
[mapper [publishDate3] has different [format] values]
	at org.elasticsearch.index.mapper.FieldTypeLookup.validateField(FieldTypeLookup.java:160) ~[elasticsearch-6.4.3.jar:6.4.3]
	at org.elasticsearch.index.mapper.FieldTypeLookup.copyAndAddAll(FieldTypeLookup.java:108) ~[elasticsearch-6.4.3.jar:6.4.3]
	at org.elasticsearch.index.mapper.MapperService.internalMerge(MapperService.java:440) ~[elasticsearch-6.4.3.jar:6.4.3]
	at org.elasticsearch.index.mapper.MapperService.internalMerge(MapperService.java:355) ~[elasticsearch-6.4.3.jar:6.4.3]
...
...
...
项目启动时的报错

原因

原因在于es服务端的mapping中,字段的type=Date不匹配
项目中使用了ElasticsearchTemplate esTemplate;
其会在启动时连接es,核对字段的类型

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## es server端mapping是date类型
"publishDate": {"type": "date" }
## 代码中的字段类型为 type = FieldType.Date, format = DateFormat.custom, pattern ="yyyy-MM-dd"
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Document(indexName = "book", type = "sogou")
public class Book2Do {
    @Id
    String id;
    @Field(index = true, type = FieldType.Text)
    String title;
    @Singular
    List<String> authors;
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern ="yyyy-MM-dd")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd", timezone = "GMT+8")
    Date publishDate;
}

解决办法

让注解的配置 @Field(type = FieldType.Date, format = DateFormat.custom, pattern ="yyyy-MM-dd”)
与es服务端一致 {“type”: “date”, “type”: “yyyy-MM-dd” }

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
PUT book
{"settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  } ,
 "mappings": {
  "sogou": {
    "properties": {
       "title": {"type": "text" }
      ,"authors":{"type":"text" }
      ,"publishDate": {"type": "date", "type": "yyyy-MM-dd" }
    }
  } 
}}