【发布时间】:2018-06-06 00:29:24
【问题描述】:
我想定期更新elasticsearch中的数据。
在我发送更新的文件中,可能有elasticsearh中已经存在的数据(更新)和新文档的数据(插入)。
由于elasticsearch中的数据是由自动创建的ID管理的, 我必须通过“代码”(唯一)列搜索 ID,以确保文档是否已经存在,如果存在则更新,否则插入。
不知道有没有比我想到的下面的代码更快的方法。
es = Elasticsearch()
# get doc ID by searching(exact match) a code to check if ID exists
res = es.search(index=index_name, doc_type=doc_type, body=body_for_search)
id_dict = dict([('id', doc['_id'])]) for doc in res['hits']['hits’]
# if id exists, update the current doc by id
# else insert with auto-created id
If id_dict['id']:
es.update(index=index_name, id=id_dict['id'], doc_type=doc_type, body=body)
else:
es.index(index=index_name, doc_type=doc_type, body=body)
例如,是否有一种方法可以让 elasticsearch 为您搜索完全匹配的 col["code"],并且您可以简单地“更新”数据而不指定 id?
任何建议将不胜感激,并感谢您的阅读。
ps-如果我们把id = col["code"]做成id = col["code"]它可能会更简单更快,但是对于管理问题我们目前还做不到。
【问题讨论】:
-
您可以告诉 elasticsearch 使用您自己的 ID。这样你就可以只索引已知 ID 的文档,它将被更新
标签: python elasticsearch