在Elasticsearch中更新文档时出错

时间:2016-09-22 13:21:35

标签: python elasticsearch

我正在尝试更新Elasticsearch中的少量文档。我想更新几个映射类型为long的字段的值。当前这些字段的值为null。

Python脚本:

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)

但是我收到了这个错误:

File "temp_updates.py", line 32, in <module>
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')

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我正在做的这是一个小错误。当我在Elasticsearch中查询并更新字段值时,我应该只索引“_source”,而不是整个文档,其中包含一些额外的字段,如“index” ,“拿”等。

因此,这应该是代码更改:

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)