python 使用帮助器进行批量弹性上载的状态更新

o4tp2gmn  于 2023-01-19  发布在  Python
关注(0)|答案(1)|浏览(134)

我试图更新一堆记录在一个单一的索引,但我需要的状态,如果更新成功与否。原因是,特定的id可能无法在索引。这是我的代码看起来像,即使我的索引中的记录正在更新,我也没有得到“update_response”的任何输出。我不应该得到“它返回一个包含摘要信息的元组-成功执行的操作数以及错误列表或错误数(如果stats_only设置为True)。
根据本页的功能定义https://elasticsearch-py.readthedocs.io/en/7.x/helpers.html

es = config.createESConnection()
start_time = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
api_url = 'abcd'
parameters = { 
    "processed":False,
    "date":start_time
}
response = requests.get(api_url,params=parameters)
info= requests.get(api_url,params=parameters).json()['data']

es_data=[]
for i in range(len(info)):
    action = {
            "_index": "index",
            "_id" : info[i]['id'],
            "_op_type":"update",
            "doc":{"x" : info[i]['x']}
        }
    es_data.append(action)
    
update_response = helpers.bulk(es, es_data,stats_only =True)
print(update_response)
except Exception:
n3h0vuf2

n3h0vuf21#

好的,这是为了帮助其他人,以防helpers.bulk中有错误,你需要启用raise_on_error=False,这样做将完成所有记录的更新,跳过有错误的记录,然后你得到返回对象。
所以用这个

update_response = helpers.bulk(es, es_data,raise_on_error=False)

相关问题