java - ElasticSearch Spring-Data 日期格式总是很长

标签 java spring elasticsearch

当使用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
 }

而且日期字段都是Long类型,我怎样才能得到日期格式:'2015-08-17T12:00:00.000'?

最佳答案

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

还有一些alternative solutions这可能更适合您的情况(即创建 CustomDateSerializer 等)。

关于java - ElasticSearch Spring-Data 日期格式总是很长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32042430/

相关文章:

java - Spring boot 使用的默认值 - spring-boot-starter-parent

Elasticsearch 集群在添加节点后不重新平衡

java - 如何防止Mysql Connector/J转换DATE和TIME时区?

Java:从链表中查找和删除元素的最佳方法

java - 是否可以使用 Turbo C/C++ 编译器生成 DLL?

elasticsearch - 在ElasticSearch中不带映射的带斜杠的值的术语过滤器

curl - 你如何让logstash输出到私有(private)IP上的elasticsearch?

java - 按降序打印奇数

java - Cloudfroundry 本地 MongoDB 部署

spring - 在 Scala 中使用 Spring @Transactional