2015-08-17 15 views
10

Khi sử dụng dữ liệu xuân để chèn tài liệu Elasticsearch với loại ngày, tôi không thể có định dạng ngày đúng, định dạng ngày luôn dài.Định dạng ngày mùa xuân dữ liệu tìm kiếm ElasticSearch luôn dài

đây là mã 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 
} 

Đây là thử nghiệm

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

tôi có thể nhận các bản đồ của thực thể được tạo ra:

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

Tuy nhiên, khi tôi tìm kiếm nó curl -X GET /entity-index/_search, tôi nhận được tài liệu sau:

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

và các trường ngày đều là loại dài, làm cách nào để có định dạng ngày: '2015-08-17T12: 00: 00.000'?

+0

Vui lòng thử định dạng ngày đúng 'yyyy-MM-dd'T'HH: mm: ss.SSSZZ', nghĩa là giờ phải được đặt trên và không có dấu tích nào xung quanh múi giờ' Z' và 'Z' phải được tăng gấp đôi . Bạn cũng có thể sử dụng định dạng 'date_time'. Lưu ý rằng trước tiên bạn cần quét sạch chỉ mục của mình để kiểm tra thay đổi này. – Val

+0

Cảm ơn câu trả lời của bạn, tôi đã thử đề xuất của bạn, xóa chỉ mục, thay đổi mẫu, nhưng vẫn hiển thị dài như dấu thời gian – fudy

+1

Trên thực tế, ánh xạ của bạn được tạo chính xác. Vấn đề có nhiều khả năng đến từ trình serializer JSON JSON. Bạn nên thử thêm chú thích này vào các trường ngày của bạn: '@JsonFormat (shape = JsonFormat.Shape.STRING, pattern =" yyyy-MM-dd'T'HH: mm: ss.SSSZZ ")'. Xem thêm một số [giải pháp thay thế] (http://www.baeldung.com/jackson-serialize-dates) có thể phù hợp hơn với trường hợp của bạn. – Val

Trả lời

12

Ánh xạ của bạn được tạo chính xác. Vấn đề có nhiều khả năng đến từ trình serializer JSON JSON. Bạn nên thử thêm chú thích này vào các trường ngày của bạn: @JsonFormat (shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ").

Cũng có một số alternative solutions có thể phù hợp hơn với trường hợp của bạn (ví dụ: tạo một CustomDateSerializer, v.v.).

Các vấn đề liên quan