How to run _update elasticsearch using an external script

I want to run an update example

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "script" : "ctx._source.text = \"some text\"" }' 

( http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-update.html ) but received the error {"error":"ElasticsearchIllegalArgumentException[failed to execute script]; nested: ScriptException[dynamic scripting for [mvel] disabled]; ","status":400} .

From this page http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html I found out that I need to place my script (I named it demorun.groovy ) and run it by name . I did this, and now try to refer as

 curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "script" : "demorun.groovy" }' 

But still get the same error. I think this is wrong. How to pass _update with an external script?

My demorun.groovy:

 ctx._source.text = \"some text\" 
+6
source share
4 answers

The error message that you receive indicates that the dynamic script is disabled, which is the default setting. You need to enable it to make scripts work:

Enable dynamic scripting

We recommend running Elasticsearch behind an application or proxy that protects Elasticsearch from the outside world. If users are allowed to run dynamic scripts (even in a search request), then they have the same access to your mailbox as the user who runs Elasticsearch as. For this reason, dynamic scripting is allowed only for the sandbox by default.

Firstly, you should not run Elasticsearch as the root user, as this will allow the script to access or do anything on your server without restriction. Secondly, you should not expose Elasticsearch directly but instead there is a proxy application between them. If you do, you intend to expose Elasticsearch directly to your users, then you have to decide whether you trust them enough to run scripts on your mailbox or not. If you do this, you can enable dynamic scripting by adding after installation to the config / elasticsearch.yml file on each node:

script.disable_dynamic: false

Although this still allows the execution of named scripts provided in config or native Java scripts registered through plugins, it also allows users to run arbitrary scripts through the API. Instead of sending the file name as a script, you can send the body of the script instead.

There are three possible configuration values โ€‹โ€‹for script.disable_dynamic, the default value is sandbox:

true: all dynamic scripts are disabled, scripts must be placed in config / scripts.

false: all dynamic scripts are enabled, scripts can be sent as strings in requests.

sandbox: scripts can be sent as strings for languages โ€‹โ€‹that are sandboxed.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html

+10
source

The problem with the ES request above is that it is using the wrong format.

Dynamic scripting (i.e. built-in scripting):

 curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "script" : "ctx._source.text = \"some text\"" }' 

Static scripting (e.g. stand-alone scripting):

 curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "script" : { "lang": "groovy", "script_file": "some-name", "params": { "foo": "some text" } } }' 

And you have to put

 ctx._source.text = foo 

in .../elasticsearch/config/scripts/some-name.groovy to reproduce almost the same functionality. Actually, itโ€™s much better because you donโ€™t have to open ES for dynamic scripts and you get argument passing.

+4
source

In elasticsearch 2.0, script.disable_dynamic: false does not work because:

An exception in the "main" thread java.lang.IllegalArgumentException: script.disable_dynamic is an unsupported setting, replac e fine-grained script settings. Dynamic scripts can be enabled for all languages โ€‹โ€‹and all operations by replacing script.disable_dynamic: false with s cript.inline: on and script.indexed: on in elasticsearch.yml

as the error says, you need to set below in elasticsearch.yml :

 script.inline: on script.indexed: on 
+2
source

I ran into the same problem and resolved it by adding the following code to the elasticsearch.yml file in the config folder:

script.disable_dynamic: false

0
source

Source: https://habr.com/ru/post/973777/


All Articles