I have a elasticsearch document in the following format. I need to partial update the "x" field and add a python dict in 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 am getting this error:
elasticsearch.exceptions.RequestError: TransportError(400, u'ElasticsearchIllegalArgumentException[failed to execute script]; nested: ScriptException[dynamic scripting for [groovy] disabled]; ')
What is the right way to do partial update in elasticsearch using python?
解决方案
For future reference, the following method of partial update worked.
elasticsearch.update(index='gdata34', doc_type='gdat', id='328091-72341-7',
body={
'doc': {'x': {'y':'z'}}
}
)