我只是試圖創建或替換文檔,但API文檔不清楚如何執行此操作。如何在Elasticsearch中創建或替換文檔?
的upsert示例顯示了一個文件與一counter
價值1
創建:
client.update({
index: 'myindex',
type: 'mytype',
id: '777',
body: {
script: 'ctx._source.counter += 1',
upsert: {
counter: 1
}
}
}, function(error, response) {
// ...
})
我不需要增量腳本,所以我儘量只通過自身發送UPSERT,但我得到一個400 Validation Failed: 1: script or doc is missing
:
client.update({
index: "testindex",
type: "testType",
id: 1234,
body: {
upsert: {
itworks: true,
ok: "oh yeah"
}
}
})
好吧,讓我們嘗試一些愚蠢的,包括DOC兩次;一旦下doc
,如果upsert
下有一個,而一旦這大概會取代現有的文件,該文件將被用於否則創建一個新文件:
client.update({
index: "testindex",
type: "testType",
id: 1234,
body: {
upsert: {
itworks: true,
ok: "oh yeah"
},
doc: {
itworks: true,
ok: "oh yeah"
}
}
})
也就是說實際工作,201創建。 200確定,當我重複一遍用不同的一個替換文檔:
client.update({
index: "testindex",
type: "testType",
id: 1234,
body: {
upsert: {
whatever: "man"
},
doc: {
whatever: "man"
}
}
})
但是,當我檢查了DOC(client.get({index: "testindex", type: "testType", id: 1234})
),我看到的不是替換原有文件,新的文檔與它合併。
如何用標準的Node客戶端替換Elasticsearch中的文檔? HTTP API使它看起來so simple,但我已經嘗試了一些在Node客戶端中的排列而沒有成功。有沒有替換命令?我必須刪除然後創建嗎?
具有相同ID的'create'調用返回409衝突:文檔存在,或者版本衝突,如果包含與現有版本不匹配的版本。 –
嘗試'索引'而不是'創建' –
就是這樣!我從來不會猜到這個詞。我在考慮如何在RDBMS中使用它們的索引,它們大多隻是一種優化。 –