Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 579
Pankaj Garg Avatar asked Feb 10 '15 14:02

Pankaj Garg


1 Answers

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.

like image 97
Heschoon Avatar answered Sep 18 '22 11:09

Heschoon