使用 Elasticsearch 中的脚本进行更新

San*_*ery 5 python scripting elasticsearch

我正在尝试使用 Elasticsearch 中的脚本来更新一些数据。我的脚本如下:

    for i in df.index:
        es.update(
            index=indexout,
            doc_type="suggestedTag",
            id=df['dataId'][i],
            _source=True,
            body={
                "script": {
                    "inline": "ctx._source.items.suggestionTime = updated_time",
                    "params": {
                        "updated_time": {
                            "field": df['suggestionTime'][i]
                        }
                    }
                }
            }
        )
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,我收到以下错误:

引发HTTP_EXCEPTIONS.get(status_code,TransportError)(status_code,error_message,additional_info)elasticsearch.exceptions.RequestError:RequestError(400,'illegal_argument_exception','[jLIZdmn] [127.0.0.1:9300] [索引:数据/写入/更新[ s]]')

我已经查看了这个问题以启用它,但即使有了这个和文档,它仍然会引发相同的错误。我在 config/elasticsearch.yml 文件中插入了以下元素:

script.inline: true
script.indexed: true
script.update: true 
Run Code Online (Sandbox Code Playgroud)

但我仍然无法避免从一开始就有的RequestError

Nik*_*iev 4

你就快到了,只需要params.在之前添加updated_time

{
  "script": {
    "inline": "ctx._source.items.suggestionTime = params.updated_time",
    "params": {
      "updated_time": {
        "field": df['suggestionTime'][i]
      }
    }
  }
} 
Run Code Online (Sandbox Code Playgroud)

如果您尝试在Kibana 控制台中运行查询,它将如下所示:

POST /my-index-2018-12/doc/AWdpylbN3HZjlM-Ibd7X/_update
{
  "script": {
    "inline": "ctx._source.suggestionTime = updated_time",
    "params": {
      "updated_time": {
        "field": "2018-10-03T18:33:00Z"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

您将看到 Elasticsearch 的完整响应,看起来像您的错误消息 + 有价值的详细信息:

{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[7JNqOhT][127.0.0.1:9300][indices:data/write/update[s]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "failed to execute script",
    "caused_by": {
      "type": "script_exception",
      "reason": "compile error",
      "script_stack": [
        "... _source.suggestionTime = updated_time",
        "                             ^---- HERE"
      ],
      "script": "ctx._source.suggestionTime = updated_time",
      "lang": "painless",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Variable [updated_time] is not defined."
      }
    }
  },
  "status": 400
}
Run Code Online (Sandbox Code Playgroud)

这向我们指出了语法错误(参数显然是作为paramsobject注入的)。

我相信在这种情况下脚本设置不是问题的根源。

希望有帮助!