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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32042430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:33:13  来源:igfitidea点击:

ElasticSearch Spring-Data Date format always is long

javaspringelasticsearch

提问by fudy

When using spring-data to insert Elasticsearch document with Date type, I can't get right date format, the date format always is Long.

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

here is the java code: Entity.java

这是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.Hymanson.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 
}

Here is the Test

这是测试

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);
    }

I can get the mapping of the created entity:

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

{
   "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"
               }
            }
         }
      }
   }
}

However, when I search it curl -X GET /entity-index/_search, I get the following document:

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

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

and the Date Fields are all Long type, how can I get the date format : '2015-08-17T12:00:00.000'?

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

采纳答案by Val

Your mapping is created correctly. The problem is more likely to come from the Hymanson JSON serializer. You should try adding this annotation to your date fields: @JsonFormat (shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ").

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

There are also some alternative solutionsthat might better suit your case (i.e. creating a CustomDateSerializer, etc).

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

回答by Wilson Paul

It worked for me with below settings. Note: delete your index before to test this change. Make sure that the patterns are same at all places.

它使用以下设置对我有用。注意:在测试此更改之前删除您的索引。确保所有地方的图案都相同。

@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'")
private Date date;