python - 在Elasticsearch中更新文档时出错

标签 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')

我想念什么?

最佳答案

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

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

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)

关于python - 在Elasticsearch中更新文档时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39640268/

相关文章:

python - 给定周内的天数/日期

python - sublimetext 3 按回车键禁用自动完成

C# 嵌套、 Elasticsearch : update and add to a field that is a list

elasticsearch - 获取由multiJson对象创建的PostData的字符串(json)表示形式

arrays - Elasticsearch:按数组中的最大值排序

python - 对数正态pdf在python中生成零

python - 同时循环三个列表 : nested loop not working

c# - elasticsearch.net 和 Nest (C#) 中的日期格式问题

python - 如何将提取的数据转换成python字典?

python - 如何使用 Django 创建订阅服务