使用Elastisearch中的脚本进行更新

时间:2018-12-03 13:35:50

标签: python elasticsearch scripting

我正在尝试在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]
                        }
                    }
                }
            }
        )

但是当我这样做时,出现以下错误:

  

提高HTTP_EXCEPTIONS.get(状态代码,TransportError)(状态代码,error_message,Additional_info)elasticsearch.exceptions.RequestError:RequestError(400,'illegal_argument_exception','[jLIZdmn] [127.0.0.1:9300] [索引:数据/写入/ update [s]]')

我已经看过this question来启用它,但是即使使用了the documentation,它仍然会引发相同的错误。我在config / elasticsearch.yml文件中插入了以下元素:

script.inline: true
script.indexed: true
script.update: true 

但是我仍然无法避免自开始以来遇到的RequestError

1 个答案:

答案 0 :(得分:1)

您快到了,只需在params.之前添加updated_time

{
  "script": {
    "inline": "ctx._source.items.suggestionTime = params.updated_time",
    "params": {
      "updated_time": {
        "field": df['suggestionTime'][i]
      }
    }
  }
} 

如果您尝试在Kibana console中运行查询,它将看起来像这样:

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"
      }
    }
  }
}

您将看到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
}

这使我们指出了语法错误(参数,显然是are injected as params object)。

在这种情况下,我认为脚本设置不是问题的根源。

希望有帮助!