【问题标题】:Can I update by query in ElasticSearch Bulk Api?我可以通过 ElasticSearch Bulk Api 中的查询进行更新吗?
【发布时间】:2017-06-02 19:49:12
【问题描述】:

假设我有 1000 个用户,每个用户有 50 个文档(每个文档嵌入具有姓名和电子邮件的用户对象),我需要更新一组用户的姓名和电子邮件。

我想使用 Bulk Api,但好像 bulk API 只支持“_id”作为参数。我想改为查询

例如:我想做,

POST _bulk
{ "update" : {"query" : {"terms": {"userId": "25"}}, "_type" : "comments", "_index" : "comments"} }
{ "doc" : {"user.name" : "new name"} }

而不是

POST _bulk
{ "update" : {"_id" : "1", "_type" : "comments", "_index" : "comments"} }
{ "doc" : {"user.name" : "new name"} }

【问题讨论】:

    标签: elasticsearch elasticsearch-bulk-api


    【解决方案1】:

    您可以为此使用update by query API

    POST comments/_update_by_query
    {
      "script": {
        "inline": "ctx._source.user.name = newName",
        "lang": "painless",
        "params": {
          "newName": "new name"
        }
      },
      "query": {
        "term": {
          "userId": "25"
        }
      }
    }
    

    【讨论】:

    • 我需要对所有用户都做同样的事情,我可以在批量 api 中放入相同的查询吗?
    • 如果您需要为所有用户执行此操作,您不需要编写脚本,您可以简单地通过批量 API 发布部分文档更新。
    • Bulk API 我们需要提到文档的id,但是我没有id。
    • 那么您就无法使用批量 API。新名称和 userId 来自哪里?
    • 它们来自另一个微服务
    猜你喜欢
    • 1970-01-01
    • 2017-08-18
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-02
    • 1970-01-01
    • 2017-01-02
    相关资源
    最近更新 更多