elasticsearch自动填充时间,并转换为上海时区

由于ES默认时区为UTC,比我上海时区慢八个小时且是TZ格式,当我们需要自动填充为yyyyy-MM-dd HH:mm:ss格式时我们就需要对其进行处理。

1、定义预处理管道:chage_utc_to_asiash(管道名可更改)

PUT _ingest/pipeline/chage_utc_to_asiash
{
  "processors": [
    {
      "script": {
        "lang": "painless",
        "source": """
                def imp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
          imp.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            def ts = imp.format(new Date((new Date().getTime())));
            ctx.update_time = ts;

        """
      }
    }
  ]
}

2、创建索引,指定管道

PUT /teacher
{
  "settings": {
    "default_pipeline": "chage_utc_to_asiash"
  }, 
  "mappings": {
    "properties": {
      "name":{
        "type": "text",
        "analyzer": "standard"
      },
      "address":{
        "type": "text"
        , "analyzer": "standard"
      }
    }
  }
}

3、写入数据

PUT teacher/_doc/1
{
  "name":"张三",
  "address":"上海"
}

4.查看结果

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值