版本
springboot 2.0.4升级为2.2.2
elasticsearch 6.4升级为7.5
某些原因,需要将elasticsearch升级为7.x版本,springboot在2.2.x版本开始支持,所以这次双升级
原因
在项目用到的elasticsearch依赖如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
model层
@Data
@Document(indexName = "user", type = "_doc", shards = 5, replicas = 1, indexStoreType = "fs", refreshInterval = "-1")
public class User {
@Id
private String id;
private String name;
@Field(type = FieldType.Date ,store = true, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
@JsonProperty(value = "createtime")
private Date ctime; //创建时间
}
service层
@Test
public void addTest(){
User user = new User();
user.setId("2");
user.setName("jimu");
user.setCtime(new Date());
userRepository.save(user);
}
在执行插入操作后,发现数据可以正常插入到es中,但是插入的类型只有Id和Name,Ctime并没有插入成功,随后查看index mapping发现mapping如下:
//插入数据结果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "user",
"_type": "_doc",
"_id": "2",
"_score": 1,
"_source": {
"id": "2",
"name": "jimu" //这里并没有我们想要的createtime
}
}
]
}
}
//mapping
{
"user": {
"mappings": {
"properties": {
"ctime": { //可以注意到这里创建的时ctime,但实际上应该是createtime
"type": "date",
"store": true,
"format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
经过测试,发现问题出在@JsonProperty(value = "createtime")
这个注释上,单纯将这个对象json格式化会发现其实是生效的,但是插入es时失效,这个问题在es6.4的版本并没有出现。
解决方法
- 如果已经有正在用的index,可以先新建index和mapping,随后用reindex将数据导入新的index中
- 代码层面去掉
@JsonProperty(value = "createtime")
这个注释,可以将原ctime
改为createtime
修改后代码
@Field(type = FieldType.Date ,store = true, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
//@JsonProperty(value = "createtime")
private Date createtime; //创建时间
mapping如下
{
"user": {
"mappings": {
"properties": {
"createtime": { //此时mapping正常,数据也可以正常插入
"type": "date",
"store": true,
"format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
reindex
[post] http://127.0.0.1:9200/_reindex
{
"source": {
"index": "user"
},
"dest": {
"index": "user_v2"
}
}