Pankaj Garg
Pankaj Garg

Reputation: 1003

RequestError while updating the index in elasticsearch

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?

Upvotes: 6

Views: 11399

Answers (1)

Heschoon
Heschoon

Reputation: 3019

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.

Upvotes: 14

Related Questions