如何更新 elasticsearch 中的时间戳范围?

How do I update range of timestamps in elasticsearch?

我存储在 elastic 中的一些数据移动到很远的未来,因为我混淆了秒和毫秒的东西。我需要将指定范围内的所有时间戳除以 1000。

我知道如何获取所需的数据,但我不知道如何更新它们。

GET /_search
{
  "query": {
    "range": {
      "message.timestamp": {
        "gte": "+50798-06-20T00:27:37"
      }
    }
  }
}

我找到了答案。

使用POST发送更新请求。我使用 _update_by_query 请求,指定范围并编写了一个脚本,将字符串日期时间解析为时间戳,然后除以 1000 以转换为秒。

POST myindex/_update_by_query
{
  "query": {
    "range": {
      "message.timestamp": {
        "gte": "+50000-06-20T00:27:37"
      }
    }
  },
  "script": {
    "source": "SimpleDateFormat format = new SimpleDateFormat(\"+yyyyy-MM-dd'T'HH:mm:ss\", Locale.getDefault()); long timestamp = format.parse(ctx._source.message.timestamp).getTime(); ctx._source.message.timestamp = timestamp / 1000;",
    "lang": "painless"
  }
}