[英]How can I do upsert (update and insert) query in MySQL Python?
[英]how can I upsert documents in opensearch by python api
我試圖將文檔更新到 Opensearch,這意味着如果不存在 id,它將插入文檔,如果 id 已經存在,那么它將更新文檔(不被覆蓋)。
例如,如果文檔已經在 Opensearch {"id":1,"name":"Jack"}
中,當我插入{"id":1,"job":"engineer"}
時,opensearch 中的文檔將變為{"id":1,"name":"Jack","job":"engineer"}
,不只是被覆蓋。
我嘗試了 python 索引 api 與 doc_as_upsert 如下,但它失敗了:
pyClient.index(
index = indexName,
body = document,
id = document['id'],
refresh = True,
doc_as_upsert = True
)
文檔 object 是:{"id":"123","name":"Jack","job":"Engineer"}
嘗試使用 index = document['_index'],body = document[_source] 而不是 index = indexName,body = document
index
,您應該使用update
方法doc_as_upsert
。 update
function 的文檔強調了這一事實。通過它的正確方法是:
document = {"id":1,"job":"engineer"}
body = {"doc": document, "doc_as_upsert": True}
pyClient.index(
index = indexName,
body = body,
id = document['id'],
refresh = True
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.