【ElasticSearch】索引日期格式处理

一.问题背景

    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
        }
      },

 

  

posted @ 2021-11-01 10:20  听风是雨  阅读(2815)  评论(0编辑  收藏  举报
/* 看板娘 */