elasticsearch-painless - 操纵日期

Era*_* H. 6 elasticsearch elasticsearch-painless

我试图在elasticsearch的脚本语言中操纵日期painless.具体来说,我试图增加4小时,即14,400秒.

{
  "script_fields": {
    "new_date_field": {
      "script": {
        "inline": "doc['date_field'] + 14400"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这引发了 Cannot apply [+] operation to types [org.elasticsearch.index.fielddata.ScriptDocValues.Longs] and [java.lang.Integer].

谢谢

Era*_* H. 22

解决方案是使用 .value

{
  "script_fields": {
    "new_date_field": {
      "script": {
        "inline": "doc['date_field'].value + 14400"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,我实际上想用它来重新索引,格式有点不同.这是我在_reindexapi中操纵时间的版本

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);"
  }
}
Run Code Online (Sandbox Code Playgroud)

这是可读版本的内联脚本

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);
Run Code Online (Sandbox Code Playgroud)

是无痛支持的功能列表,很痛苦.

  • 对于我来说,处理日期字符串格式的小数秒(2018-05-28T13:49:40.03Z)是非常有用的.repo的链接帮助我了解java.time类(特别是Instant)在一个无痛的脚本中得到支持,所以我可以编写`def existLastSeen = Instant.parse(ctx._source.last_seen_ts); def thisLastSeen = Instant.parse(params.last_seen_ts); if(thisLastSeen.isAfter(existLastSeen)){ctx._source.last_seen_ts = params.last_seen_ts; } (2认同)