Elasticsearch partial update using python

I have an elasticsearch document in the following format. I need to partially update the "x" field and add python python to it.

{ "_index": "gdata34", "_type": "gdat", "_id": "328091-72341-118", "_version": 1, "_score": 1, "_source": { "d": { "Thursday": { "s": "" }, "Email": { "s": "" }, "Country": { "s": "US" }, }, "x": { "Geo": { "s": "45.335428,-118.057133", "g": [ -118.057133 , 45.335428 ] } }, } } 

I tried the following code to update:

 from elasticsearch import Elasticsearch, exceptions import pprint elasticsearch = Elasticsearch() doc = elasticsearch.get(index='gdata34', doc_type='gdat', id='328091-72341-7') elasticsearch.update(index='gdata34', doc_type='gdat', id='328091-72341-7', body={"script":"ctx._source.x += y", "params":{"y":"z"} } ) elasticsearch.indices.refresh(index='gdata34') new_doc = elasticsearch.get(index='gdata34', doc_type='gdat', id='328091-72341-7') 

I get this error:

 elasticsearch.exceptions.RequestError: TransportError(400, u'ElasticsearchIllegalArgumentException[failed to execute script]; nested: ScriptException[dynamic scripting for [groovy] disabled]; ') 

What is the correct way to partially upgrade in elasticsearch using python?

+6
source share
2 answers

Subsequently, the following partial update method was used for reference.

 elasticsearch.update(index='gdata34', doc_type='gdat', id='328091-72341-7', body={ 'doc': {'x': {'y':'z'}} } ) 
+9
source

From ElasticSearch Script Documents :

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.

Now, in the latest version of ES, an error has appeared in the Groovy scripting engine vulnerability, which allows scripts to leave the sandbox and execute shell commands as the user running the Elasticsearch Java virtual machine - therefore, the Groovy sandbox is disabled by default in recent versions and, therefore, Groovy scripts are executed passed to the request body or from the .scripts index. The only way to run Groovy scripts with this default configuration is to put them in the config/scripts/ directory on node.

So, you have two options:

  • If your ES instance is not directly accessible and protected by the proxy server, you can re-enable the Groovy sandbox by setting script.groovy.sandbox.enabled: true in config/elasticsearch.yml to node (s). If your ES instance is available through
  • You can prepare your script and put it on the file system in the config/scripts directory of your node and call it by name. See Running Groovy Scripts without dynamic scripts for more details.
+1
source

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


All Articles