一.問題背景
ElasticSearch 日期處理可以使用Long類型,可以使用Date類型,Date類型方便查詢,這里記錄下Date類型的索引處理
二.代碼
1.Maven相關依賴: Springboot版本 2.2.6.RELEASE
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- es --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> </dependency>
2.索引創建
這里日期類型的字段使用 類型@Field(type = FieldType.Date)處理
同時,指定格式為 pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis" 表示可以接受多種日期格式
package com.nn.data.es.entity; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; 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.FieldType; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; /** * @author Sam.yang * @since 2021/9/4 15:20 */ @Data @Document(indexName = "index_user_real_auth_latest", type = "t_user_real_auth", shards = 5, replicas = 1, createIndex = false) public class UserAuthInfoEs { @Id private Long id; @Field(type = FieldType.Long) private Long userId; @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis") private Date authBirthTime; @Field(type = FieldType.Keyword) private String authRealName; @Field(type = FieldType.Keyword) private String createBy; @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") private Date createTime; @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") private Date updateTime; @Field(type = FieldType.Integer) private Integer authSource; @Field(type = FieldType.Integer) private Integer mainlandId; @Field(type = FieldType.Keyword) private String idNumber; @Field(type = FieldType.Integer) private Integer isDelete; }
查看Kibana中索引的mapping,如下:
{ "index_user_real_auth_latest" : { "mappings" : { "t_user_real_auth" : { "properties" : { "authBirthTime" : { "type" : "date", "format" : "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis" }, "authRealName" : { "type" : "keyword" }, "authSource" : { "type" : "integer" }, "createBy" : { "type" : "keyword" }, "createTime" : { "type" : "date", "format" : "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis" }, "id" : { "type" : "long" }, "idNumber" : { "type" : "keyword" }, "isDelete" : { "type" : "integer" }, "mainlandId" : { "type" : "integer" }, "updateTime" : { "type" : "date", "format" : "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis" }, "userId" : { "type" : "long" } } } } } }
三.測試
@Autowired private UserAuthInfoEsRepository userAuthInfoEsRepository; /** * 新增 */ @Override public void add() { for (int i = 0; i < 10; i++) { UserAuthInfoEs userAuthInfoEs = new UserAuthInfoEs(); userAuthInfoEs.setId(Long.valueOf(i)); userAuthInfoEs.setIdNumber("xxxxxxxxxxxxxxxxxxxxxx"); userAuthInfoEs.setAuthBirthTime(null); userAuthInfoEs.setCreateTime(new Date()); userAuthInfoEs.setUpdateTime(new Date()); userAuthInfoEs.setCreateBy("創建人" + i); userAuthInfoEs.setUserId(Long.valueOf(i)); userAuthInfoEs.setAuthRealName(String.valueOf(i)); userAuthInfoEs.setAuthSource(1); userAuthInfoEsRepository.save(userAuthInfoEs); } } /** * 查詢 * * @return */ @Override public UserAuthInfoEs getById() { Optional<UserAuthInfoEs> opt = userAuthInfoEsRepository.findById(0l); if (opt.isPresent()) { UserAuthInfoEs userAuthInfoEs = opt.get(); log.info("查詢結果:{}", userAuthInfoEs); } return opt.get(); }
查看Kibana中的結果:與預期的一致
hits" : [ { "_index" : "index_user_real_auth_latest", "_type" : "t_user_real_auth", "_id" : "0", "_score" : 1.0, "_source" : { "id" : 0, "userId" : 0, "authBirthTime" : null, "authRealName" : "0", "createBy" : "創建人0", "createTime" : "2021-11-01 10:15:16", "updateTime" : "2021-11-01 10:15:16", "authSource" : 1, "mainlandId" : null, "idNumber" : "xxxxxxxxxxxxxxxxxxxxxx", "isDelete" : null } },