在使用弹性 Upsert 时,可以通过以下方式避免冲突:
from elasticsearch import Elasticsearch
es = Elasticsearch()
doc = {"title": "Elasticsearch Guide", "author": "John Smith"}
es.index(index="my_index", doc_type="_doc", id=1, body=doc)
updated_doc = {"title": "Elasticsearch Guide 2.0", "author": "Jane Doe"}
es.index(index="my_index", doc_type="_doc", id=1, body=updated_doc)
res = es.update(
index='my_index',
doc_type='_doc',
id=1,
body={
'doc': {"title": "Elasticsearch Guide 2.0", "author": "Jane Doe"},
'retry_on_conflict': 3
}
)
actions = [
{
'_op_type': 'update',
'_index': 'my_index',
'_type': '_doc',
'_id': 1,
'doc': {"title": "Elasticsearch Guide 2.0", "author": "Jane Doe"},
'upsert': {"title": "Elasticsearch Guide", "author": "John Smith"}
}
]
res = helpers.bulk(es, actions)