简体   繁体   English

如何通过 python api 在 opensearch 中插入文档

[英]how can I upsert documents in opensearch by python api

I tried to upsert a document to Opensearch, that means if no id exists it would insert the document, if the id already exists then it would update the document(not overwritten).我试图将文档更新到 Opensearch,这意味着如果不存在 id,它将插入文档,如果 id 已经存在,那么它将更新文档(不被覆盖)。

For example, if the doc already in Opensearch {"id":1,"name":"Jack"} , when I upserted {"id":1,"job":"engineer"} , the document in opensearch would become {"id":1,"name":"Jack","job":"engineer"} , not just overwritten.例如,如果文档已经在 Opensearch {"id":1,"name":"Jack"}中,当我插入{"id":1,"job":"engineer"}时,opensearch 中的文档将变为{"id":1,"name":"Jack","job":"engineer"} ,不只是被覆盖。

I tried python index api with doc_as_upsert as following, but it failed:我尝试了 python 索引 api 与 doc_as_upsert 如下,但它失败了:

pyClient.index(
            index = indexName,
            body = document,
            id = document['id'],
            refresh = True,
            doc_as_upsert = True
        )

The document object is: {"id":"123","name":"Jack","job":"Engineer"}文档 object 是:{"id":"123","name":"Jack","job":"Engineer"}

try with index = document['_index'],body = document[_source] instead of index = indexName,body = document尝试使用 index = document['_index'],body = document[_source] 而不是 index = indexName,body = document

  1. Instead of index , you should use the update method而不是index ,您应该使用update方法
  2. You're passing the doc_as_upsert incorrectly.您错误地传递了doc_as_upsert The documentation for the update function highlights that fact. update function 的文档强调了这一事实。

The correct way to pass it would be:通过它的正确方法是:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM