【问题标题】:How to create or replace a document in Elasticsearch?如何在 Elasticsearch 中创建或替换文档?
【发布时间】: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


    【解决方案1】:

    在 Elasticsearch 中,要替换文档,您只需索引具有相同 ID 的文档,它将自动替换。

    如果您想更新文档,您可以执行脚本更新、部分更新或两者兼而有之。

    要进行部分文档更新,您只需

    部分文档更新:

    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 documentation

    【讨论】:

    • 具有相同 ID 的 create 调用返回 409 冲突:如果我包含与现有版本不匹配的版本,则文档存在或版本冲突。
    • 尝试使用index 而不是create
    • 就是这样!我永远不会猜到这个词。我在考虑索引,比如它们在 RDBMS 中的使用方式,它们大多只是一种优化。
    【解决方案2】:

    这是我总是使用 index 而不是 create 的原因之一:

    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
        }
    });
    

    【讨论】:

    猜你喜欢
    • 2018-04-30
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 2020-05-04
    • 2010-11-03
    • 1970-01-01
    • 2019-11-30
    • 2021-03-06
    相关资源
    最近更新 更多