[英]elasticsearch.js: updateByQuery if exist otherwise insert
使用 elasticsearch.js (node.js) SDK 我們可以使用client.index({... })
插入文檔(如果不存在則插入,否則更新)。
但是,如果我只想更新特定字段,例如這樣:
client.updateByQuery({
index: 'test',
body: {
query: { match: { _id: '1' } },
script: { inline: 'ctx._source.hello = "world"' }
}
})
現在我想創建一個 id 為 1 的文檔,如果它的字段 hello 設置為 world 不存在,或者如果 id 為 1 的文檔存在,則將其 hello 字段更新為 world。 這可能嗎? 如何?
您可能正在尋找更新 API 與 doc 作為 update 。 使用如下:
POST test/_update/1
{
"doc":{
"hello": "world"
},
"doc_as_upsert": true
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.