zoukankan      html  css  js  c++  java
  • 【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
            }
          },

      

  • 相关阅读:
    深入浅出百度地图API开发系列(3):模块化设计
    深入浅出百度地图API开发系列(2):创建地图
    深入浅出百度地图API开发系列(1):前言
    使用分页助手pageHelper,方便快捷
    mybatis注解模糊查询的两种方式
    idea svn 提交拉取代码抛出 Error:'C:Program' 不是内部或外部命令,也不是可运行的程序 或批处理文件
    Could not open ServletContext resource [/WEB-INF/applicationContext.xml]”解决方案
    com.sun.jersey.api.client.UniformInterfaceException
    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
    继承关系,子类初始化,类中成员的加载顺序
  • 原文地址:https://www.cnblogs.com/july-sunny/p/15492341.html
Copyright © 2011-2022 走看看