我正在尝试使用Elasticsearch的脚本语言来操纵日期painless。具体来说,我尝试增加4小时,即14,400秒。
painless
{ "script_fields": { "new_date_field": { "script": { "inline": "doc['date_field'] + 14400" } } } }
这抛出 Cannot apply [+] operation to types [org.elasticsearch.index.fielddata.ScriptDocValues.Longs] and [java.lang.Integer].
Cannot apply [+] operation to types [org.elasticsearch.index.fielddata.ScriptDocValues.Longs] and [java.lang.Integer].
谢谢
解决方案是使用 .value
.value
{ "script_fields": { "new_date_field": { "script": { "inline": "doc['date_field'].value + 14400" } } } }
但是,我实际上想将其用于重新索引,但格式略有不同。这是我在_reindexapi中操纵时间的版本
_reindex
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);" } }
这是可读版本的内联脚本
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);
这是无痛支持的功能列表,很痛苦。