I have a record indexed in the elasticsearch with certain timestamp. I am trying to update the record using the following code (in python):
from elasticsearch import Elasticsearch
from datetime import datetime
import pytz
es = Elasticsearch()
time = datetime.utcnow().replace(tzinfo=pytz.utc)
msg = {'_id': 1, 'text': 'Hello World'}
es.index(index='idx', doc_type='dtype', id=msg['_id'], body=msg, timestamp=time, ttl='30d')
msg['text'] = 'New Message'
es.update(index='idx', doc_type='dtype', id=msg['_id'], body=msg, timestamp=time, ttl='30d')
And I am getting the following error:
RequestError: TransportError(400, u'ActionRequestValidationException[Validation Failed: 1: script or doc is missing;]')
What could be the reason for the same?
The message number 400 means that you have a "Bad request". The request body/URL is not what is expected.
In this case, it is due to the fact that you don't use a script or a doc in the body. Have a look at the Update API documentation for more information.
The following code solves your problem:
from elasticsearch import Elasticsearch
from datetime import datetime
import pytz
es = Elasticsearch()
time = datetime.utcnow().replace(tzinfo=pytz.utc)
msg = {'_id': 1, 'text': 'Hello World'}
es.index(index='idx', doc_type='dtype', id=msg['_id'], body=msg, timestamp=time, ttl='30d')
msg2 = '''{"doc": {"text": "New Message"}}'''
es.update(index='idx', doc_type='dtype', id=msg['_id'], body=msg2, timestamp=time, ttl='30d')
By surrounding the information you want to change by a doc
tag, you tell ElasticSearch that you want to replace the values with those of the partial document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With