需求
在ES中想储存一个映射类型为Date的字段(createdAt),格式为yyyy-MM-dd HH:mm:ss
问题
创建mapping映射时指定了format
"properties": {
"createAt": {
"type":"date","format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
实体类上加的注解
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd HH:mm:ss", timezone ="GMT+8")
@Field(type = FieldType.Date, pattern ="yyyy-MM-dd HH:mm:ss")
private Date createdAt;
构建IndexRequest的Api
报错
Elasticsearch exception [type=mapper_parsing_exception, reason=failed to parse field [createdAt] of type [date] in document with id ‘35’]
解决
IndexRequest indexRequest = new IndexRequest("blog", "_doc", blog.getId())
.source("id", blog.getId(),
"content", blog.getContent(),
"createdAt", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(blog.getCreatedAt()).toString(),
"summary", blog.getSummary(),
"title", blog.getTitle(),
"userId", blog.getUserId(),
"isFile", blog.getIsFile(),
"tags", blog.getTags());
将字段格式化为符合要求的字符串,ES就能储存了
版权声明:本文为qq_44025894原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。