ElasticSearch中Date类型转换问题

需求

在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中Date类型转换问题_第1张图片
报错
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就能储存了

你可能感兴趣的:(项目小结,elasticsearch,es,entity,java)