RequestError while updating the index in elasticsearch

10,816

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.

Share:
10,816
Pankaj Garg
Author by

Pankaj Garg

Updated on June 09, 2022

Comments

  • Pankaj Garg
    Pankaj Garg almost 2 years

    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?