簡體   English   中英

ElasticSearch Spring-Data 日期格式總是很長

[英]ElasticSearch Spring-Data Date format always is long

使用 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'?

您的映射已正確創建。 問題更有可能來自 Jackson JSON 序列化程序。 您應該嘗試將此注釋添加到您的日期字段: @JsonFormat (shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ")

還有一些替代解決方案可能更適合您的情況(即創建CustomDateSerializer等)。

如果滿足需要,我建議嘗試現有的可用映射。 使用 es 版本: 7.14.1 ,以下效果很好:

@Field(type = FieldType.Date, format = DateFormat.basic_date_time)
private Date creationTime;

好處是不必擔心映射,因為它是自動創建的。 舊版本可能支持它,我沒有檢查過。

從 Elasticsearch 7 開始,您不應該使用yyyy而是使用uuuu 例如:

@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSZZ")
private Date lastModifiedDate;

您不需要@JsonProperty因為現在 Spring Data Elasticsearch 不使用 Jackson 而是使用MappingElasticsearchConverter 有了這個注解,就會自動為這個屬性創建一個轉換器並使用它。

它適用於以下設置。 注意:在測試此更改之前刪除您的索引。 確保所有地方的圖案都相同。

@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;

要記住的事情:

JSON 沒有日期數據類型,因此 Elasticsearch 中的日期可以是:

  • 包含格式化日期的字符串,例如“2015-01-01”或“2015/01/01. 12:10:30”。
  • 一個長數字,表示自時代以來的毫秒數。
  • 一個整數,表示自紀元以來的秒數。

也來自同一個文檔:

日期將始終呈現為字符串,即使它們最初在 JSON 文檔中以 long 形式提供

這意味着查詢日期字段的數據類型將始終是字符串、長整數或整數。 彈性搜索中沒有特殊的“日期”字段。

在此處閱讀更多信息: https ://www.elastic.co/guide/en/elasticsearch/reference/current/date.html

它適用於以下設置:

@Field(name = "@timestamp", type = FieldType.Date, format = DateFormat.custom, pattern = "uuuu-MM-dd'T'HH:mm:ss.SSS'Z'")
private Date timestamp;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM