Error while updating document in Elasticsearch
<p>I am trying to update few documents in Elasticsearch.I want to update value of few fields whose mapping type is long.Currently value of those fields is null.</p>

<p>Python Script:</p>

<pre><code>def dump_random_values():

    query = {"size": 2000, "query": {"bool": {"must": [{"term": {"trip_client_id": {"value": 23}}}, {"type": {"value": "trip-details"}}]}}}
    docs = es.search(index=analytics_index, doc_type="trip-details", body=query)
    trips = docs["hits"]["hits"]
    for trip in trips:
        doc_id = trip["_id"]

        trip["_source"]["vehicle_capacityInWeight"] = random.randint(40, 50)
        trip["_source"]["shipment_packageWeight"] = random.randint(1, 39)

        trip["_source"]["vehicle_capacityInVolume"] = random.randint(40, 50)
        trip["_source"]["shipment_packageVolume"] = random.randint(1, 39)

        trip["_source"]["shipment_packageUnits"] = random.randint(40, 50)
        trip = {"doc": trip}

        es.update(index=analytics_index, doc_type="trip-details", id=doc_id, body=trip)
</code></pre>

<p>But I am getting this error :</p>

<pre><code>File "temp_updates.py", line 32, in &lt;module&gt;
dump_random_values()
File "temp_updates.py", line 30, in dump_random_values
es.update(index=analytics_index, doc_type="trip-details", id=doc_id, body=trip)
File "/Users/amanagarwal/Desktop/venv/analytics-django/lib/python3.5/site-packages/elasticsearch/client/utils.py", line 69, in _wrapped
return func(*args, params=params, **kwargs)
File "/Users/amanagarwal/Desktop/venv/analytics-django/lib/python3.5/site-packages/elasticsearch/client/__init__.py", line 460, in update
doc_type, id, '_update'), params=params, body=body)
File "/Users/amanagarwal/Desktop/venv/analytics-django/lib/python3.5/site-packages/elasticsearch/transport.py", line 329, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/Users/amanagarwal/Desktop/venv/analytics-django/lib/python3.5/site-packages/elasticsearch/connection/http_urllib3.py", line 109, in perform_request
self._raise_error(response.status, raw_data)
File "/Users/amanagarwal/Desktop/venv/analytics-django/lib/python3.5/site-packages/elasticsearch/connection/base.py", line 108, in _raise_error
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, 'mapper_parsing_exception', 'failed to parse')
</code></pre>

<p>What am I missing?</p>

<p>It was a small mistake that I was doing.When I queried in Elasticsearch and updated the field values,I should have indexed just the "_source",and not the entire document,which contains some extra fields such as "index","took" etc.</p>

<p>Hence this should be the code change:</p>

<pre><code>def dump_random_values():

    query = {"size": 2000, "query": {"bool": {"must": [{"term": {"trip_client_id": {"value": 23}}}, {"type": {"value": "trip-details"}}]}}}
    docs = es.search(index=analytics_index, doc_type="trip-details", body=query)
    trips = docs["hits"]["hits"]
    for trip in trips:
        doc_id = trip["_id"]
        current = trip["_source"]

        current["vehicle_capacityInWeight"] = random.randint(40, 50)
        current["shipment_packageWeight"] = random.randint(1, 39)

        current["vehicle_capacityInVolume"] = random.randint(40, 50)
        current["shipment_packageVolume"] = random.randint(1, 39)

        current["shipment_packageUnits"] = random.randint(40, 50)
        trip = {"doc": current}

        es.update(index=analytics_index, doc_type="trip-details", id=doc_id, body=trip)
</code></pre>

python elasticsearch