Elasticsearch 插入時間字段時數據格式問題

elasticsearch 時間格式

elasticsearch建立index的以後,能夠設置mapping
若是mapping中沒有設置date的format,那麼默認爲兩種格式
strict_date_optional_time||epoch_millis ,其中新版strict_date_optional_time移除了前綴strict_html

  • date_optional_time 此格式爲ISO8601標準 示例:2018-08-31T14:56:18.000+08:00
  • epoch_millis 也就是時間戳 示例1515150699465, 1515150699

以上2中是默認的,其餘的格式參考官方文檔-formatjava

數據插入

進入默認的格式化格式是以上兩種,那麼只用傳入對應的格式便可,想使用哪一種格式的時間,就先設置對應field的format。spring

  1. 設置格式json

    PUT my_index
    {
      "mappings": {
        "_doc": {
          "properties": {
            "date": {
              "type":   "date",
              "format": "yyyy-MM-dd"
            }
          }
        }
      }
    }
  2. 轉換java類的屬性springboot

    jackson庫在轉換爲json的時候,將Date類型轉爲爲了long型的字符串表示。咱們使用@JsonFormat註解改變格式化的結果app

    @Field(type = FieldType.Date, format = DateFormat.custom,pattern = "yyyy-MM-dd")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd",timezone="GMT+8")
    private Date callback_at;

    若是是默認的date_optional_time 的格式elasticsearch

    @Field(type = FieldType.Date, format = DateFormat.date_optional_time)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZ",timezone="GMT+8")
    private Date callback_at;

其餘格式觸類旁通便可ide

參考文檔:
springboot elasticsearch 集成注意事項ui

相關文章
相關標籤/搜索