如何在Elasticsearch中创build或replace文档?
我只是想创build或replace文档,但API文档不清楚如何做到这一点。
upsert示例显示正在创build一个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
,这将被用来创build一个新的文件,否则:
client.update({ index: "testindex", type: "testType", id: 1234, body: { upsert: { itworks: true, ok: "oh yeah" }, doc: { itworks: true, ok: "oh yeah" } } })
实际上,201创build。 200好吧,当我重复它来replace一个不同的文档:
client.update({ index: "testindex", type: "testType", id: 1234, body: { upsert: { whatever: "man" }, doc: { whatever: "man" } } })
但是,当我检查文档( client.get({index: "testindex", type: "testType", id: 1234})
),我看到,而不是replace现有的文档,新的文档被合并。
如何用标准的Node客户端replaceElasticsearch中的文档? HTTP API使它看起来非常简单 ,但是我在Node客户端中尝试了一堆排列,没有成功。 有没有replace命令? 我必须删除,然后创build?
在Elasticsearch中,要replace一个文档,你只需索引一个具有相同ID的文档,它将被自动replace。
如果您想要更新文档,您可以执行脚本更新,部分更新或两者。
要简单地做一个部分文档更新
部分文档更新:
client.update({ index: 'myindex', type: 'mytype', id: '1', body: { // put the partial document under the `doc` key doc: { title: 'Updated' } } }, function (error, response) { // ... })
脚本更新:
client.update({ index: 'myindex', type: 'mytype', id: '1', body: { script: 'ctx._source.tags += tag', params: { tag: 'some new tag' } } }, function (error, response) { // ... });
更多信息在Elasticsearch JS文档
这是我总是使用索引而不是创build的原因之一:
client.index({ "index": 'index-name', "type": 'mapping type', "id": 'id you want to create/replace', 'body': JSON.stringify(obj) }, function (err, resp) { if (err) { console.error(err['message']) } else { console.log(resp); return resp } });
- 端口9200拒绝连接?
- ElasticSearch client.indices.putMapping总是失败
- 使用ElasticSearch javascript客户端创build索引,promise不会返回
- node.js lambda函数将logging从sql迁移到elasticsearch,使用队列限制asynchronous调用
- 电子邮件的弹性search查询
- 使用节点js进行弹性searchsorting
- 如何在使用elasticjs v5.0的文档中没有id的情况下自动生成id
- 将mongodb与云托pipe的elasticsearch整合在一起
- Elasticsearch使用多个filtersearch查询