【发布时间】:2016-12-05 18:48:37
【问题描述】:
我只是想创建或替换一个文档,但 API 文档并不清楚如何执行此操作。
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 下,可能会替换现有文档(如果有的话),一次在upsert 下,否则将用于创建新文档:
client.update({
index: "testindex",
type: "testType",
id: 1234,
body: {
upsert: {
itworks: true,
ok: "oh yeah"
},
doc: {
itworks: true,
ok: "oh yeah"
}
}
})
这确实有效,201 Created。 200 OK 当我重复它以用不同的文档替换文档时:
client.update({
index: "testindex",
type: "testType",
id: 1234,
body: {
upsert: {
whatever: "man"
},
doc: {
whatever: "man"
}
}
})
但是当我检查文档 (client.get({index: "testindex", type: "testType", id: 1234})) 时,我发现新文档并没有替换现有文档,而是与其合并。
如何用标准的 Node 客户端替换 Elasticsearch 中的文档? HTTP API 使它看起来像so simple,但我在 Node 客户端中尝试了一堆排列但没有成功。没有替换命令吗?我必须先删除再创建吗?
【问题讨论】:
标签: javascript node.js elasticsearch