6

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?

3
  • Hey @mirchi, I hope my answer helped you. If that's the case, don't forget to accept if it :) Commented Mar 10, 2016 at 16:49
  • Thanks @heschoon. I am not sure how to accept the same. Commented Mar 12, 2016 at 2:46
  • to accept a question, you must click on the big green "V" under the upvote / downvote arrows on the left of the answer. It "closes" the question, marking the problem as solved. Commented Mar 13, 2016 at 19:34

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.