问题 ElasticSearch Spring-Data Date格式总是很长


当使用spring-data插入带有Date类型的Elasticsearch文档时,我无法获得正确的日期格式,日期格式始终为Long。

这是java代码:Entity.java

import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
import org.springframework.data.elasticsearch.annotations.FieldType;

import com.fasterxml.jackson.annotation.JsonProperty;

@Document(indexName = "entity-index", type = "entity-type")
public class Entity {
    @Id
    private String id;

    @Field(type = FieldType.Date, index = FieldIndex.not_analyzed, store = true, 
            format = DateFormat.custom, pattern = "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'")
    private Date createDate;

    private String system;
    private double score;

    @Field(type = FieldType.Date, format = DateFormat.date_optional_time)
    @JsonProperty(value = "@timestamp")
    private Date updateDate;
    // omit setter and getter 
}

这是测试

public class EntityDAOTest {
    @Autowired
    private ElasticsearchTemplate template;

    @Before
    public void init() {
        template.createIndex(Entity.class);
        template.putMapping(Entity.class);
    }


    @Test
    public void testCreate() {
        Entity entity = new Entity();
        entity.setId("5");
        entity.setCreateDate(new DateTime(2015,05,27,0,0).toDate());
        entity.setUpdateDate(new DateTime(2015,05,27,0,0).toDate());
        entity.setSystem("systemC");
        entity.setScore(5.7);
        IndexQuery query = new IndexQueryBuilder().withObject(entity).withId(entity.getId()).build();
        template.index(query);
    }

我可以获得创建的实体的映射:

{
   "entity-index": {
      "mappings": {
         "entity-type": {
            "properties": {
               "@timestamp": {
                  "type": "long"
               },
               "createDate": {
                  "type": "date",
                  "store": true,
                  "format": "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"
               },
               "id": {
                  "type": "string"
               },
               "score": {
                  "type": "double"
               },
               "system": {
                  "type": "string"
               },
               "updateDate": {
                  "type": "date",
                  "format": "date_optional_time"
               }
            }
         }
      }
   }
}

但是,当我搜索它 curl -X GET /entity-index/_search,我得到以下文件:

 {
               "id": "5",
               "createDate": 1432656000000,
               "system": "systemC",
               "score": 5.7,
               "@timestamp": 1432656000000
 }

并且日期字段都是长类型,如何获取日期格式:'2015-08-17T12:00:00.000'?


6290
2017-08-17 03:38


起源

请尝试使用正确的日期格式 yyyy-MM-dd'T'HH:mm:ss.SSSZZ,即小时必须是大写的,周围没有蜱 Z 时区和 Z 必须加倍。你也可以简单地使用 date_time 格式。请注意,您需要首先清除索引才能测试此更改。 - Val
感谢您的回答,我尝试了您的建议,删除了索引,更改了模式,但仍然显示为时间戳 - fudy
实际上,您的映射是正确创建的。问题更可能来自Jackson JSON序列化程序。您应该尝试将此注释添加到日期字段中: @JsonFormat (shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ")。另见一些 替代解决方案 这可能更适合你的情况。 - Val
是的,你是对的,将代码更改为:@JsonFormat(shape = JsonFormat.Shape.STRING,pattern =“yyyy-MM-dd'T'HH:mm:ss.SSS'Z'”)@JsonProperty(value =“@timestamp”)private Date updateDate;问题解决了 - fudy


答案:


您的映射已正确创建。问题更可能来自Jackson JSON序列化程序。您应该尝试将此注释添加到日期字段中: @JsonFormat (shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ")

还有一些 替代解决方案 这可能更适合你的情况(即创建一个 CustomDateSerializer等)。


13
2017-08-17 12:22



我在日期字段上使用@JsonFormat(shape = JsonFormat.Shape.STRING,pattern =“yyyy-MM-dd'T'HH:mm:ss.SSSZZ”),但它仍然失败引起:org.elasticsearch.index .mapper.MapperParsingException:无法解析[creationDate] - 由以下引起:java.lang.NumberFormatException:对于输入字符串:“2018-04-04T14:19:49”。任何选择? - Petr Kostroun
@PetrKostroun你错过了时区,删除 ZZ 从模式。 - Val