SantiStSupery
SantiStSupery

Reputation: 212

Update with scripting in Elastisearch

I am trying to use scripting in Elasticsearch to update some data. My script is the following:

    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]
                        }
                    }
                }
            }
        )

But when I do that I get the following error:

raise 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][indices:data/write/update[s]]')

And I have looked at this question to enable it, but even with this and the documentation it still raises the same error. I inserted the following elements in the config/elasticsearch.yml file :

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

But I still cannot avoid the RequestError that I have since the beginning

Upvotes: 5

Views: 3280

Answers (1)

Nikolay Vasiliev
Nikolay Vasiliev

Reputation: 6066

You are almost there, just need to add params. before updated_time:

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

If you would try to run your query in Kibana console, it would look something like this:

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

You would see the entire response of the Elasticsearch, that would look like your error message + valuable details:

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

Which points us to the syntax error (parameters, apparently, are injected as params object).

I believe the scripting settings are not the source of the problem in this case.

Hope that helps!

Upvotes: 4

Related Questions