【问题标题】:How do I overwrite the @timestamp field with another field in Elasticsearch?如何用 Elasticsearch 中的另一个字段覆盖 @timestamp 字段?
【发布时间】:2019-04-08 17:28:30
【问题描述】:

我使用错误的 @timestamp 字段错误地将大量文档提取到 Elasticsearch 中。我已经更改了受影响的 Logstash 管道以使用正确的时间戳,但我无法重新摄取旧数据。

不过,我确实有另一个文档字段可用作时间戳 (json.created_at)。所以我想更新这个领域。我发现我可以使用the _update_by_query 操作来做到这一点,但我尝试了几个不起作用的版本,包括这个:

POST logstash-rails_models-*/_update_by_query
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.@timestamp = ctx._source.json.created_at"
  }
}

这抱怨一个意外的字符:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "ctx._source.@timestamp = ctx._source. ...",
          "            ^---- HERE"
        ],
        "script": "ctx._source.@timestamp = ctx._source.json.created_at",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "compile error",
    "script_stack": [
      "ctx._source.@timestamp = ctx._source. ...",
      "            ^---- HERE"
    ],
    "script": "ctx._source.@timestamp = ctx._source.json.created_at",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "unexpected character [@].",
      "caused_by": {
        "type": "lexer_no_viable_alt_exception",
        "reason": null
      }
    }
  },
  "status": 500
}

我该怎么办?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    访问该字段的正确方法是通过括号并用引号括起来:

    POST logstash-rails_models-*/_update_by_query
    {
      "script": {
        "lang": "painless",
        "source": "ctx._source['@timestamp'] = ctx._source.json.created_at"
      }
    }
    

    另请参阅 this thread 以及有关 updating fields with Painless 的更多信息。

    【讨论】:

      猜你喜欢
      • 2012-08-14
      • 2015-01-10
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 2021-03-25
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多