elasticsearch-painless - 操作日期 [英] elasticsearch-painless - Manipulate date

查看:24
本文介绍了elasticsearch-painless - 操作日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 elasticsearch 的脚本语言 painless 操作日期.具体来说,我试图添加 4 小时,即 14,400 秒.

I am trying to manipulate date in elasticsearch's scripting language painless. Specifically, I am trying to add 4 hours, which is 14,400 seconds.

{
  "script_fields": {
    "new_date_field": {
      "script": {
        "inline": "doc['date_field'] + 14400"
      }
    }
  }
}

这会抛出 无法将 [+] 操作应用于类型 [org.elasticsearch.index.fielddata.ScriptDocValues.Longs] 和 [java.lang.Integer].

谢谢

推荐答案

解决方案是使用 .value

{
  "script_fields": {
    "new_date_field": {
      "script": {
        "inline": "doc['date_field'].value + 14400"
      }
    }
  }
}

然而,我实际上想用它来重新索引,格式有点不同.这是我在 _reindex api

However, I actually wanted to use it for reindexing, where the format is a bit different. Here is my version for manipulating time in the _reindex api

POST _reindex
{
  "source": {
    "index": "some_index_v1"
  },
  "dest": {
    "index": "some_index_v2"
  },
  "script": {
    "inline": "def sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); def dt = sf.parse(ctx._source.date_field); def calendar = sf.getCalendar(); calendar.setTime(dt); def instant = calendar.toInstant(); def localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC); ctx._source.date_field = localDateTime.plusHours(4);"
  }
}

这是可读版本的内联脚本

Here is the inline script in a readable version

def sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
def dt = sf.parse(ctx._source.date_field);
def calendar = sf.getCalendar();
calendar.setTime(dt);
def instant = calendar.toInstant();
def localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
ctx._source.date_field = localDateTime.plusHours(4);

这里是painless支持的功能列表,很痛苦.

Here is the list of functions supported by painless, it was painful.

这篇关于elasticsearch-painless - 操作日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆