简体   繁体   English

弹性 - 在单个请求中更新多个文档

[英]Elastic - updating multiple documents in a single request

I need to update several thousand items every several minutes in Elastic and unfortunately reindexing is not an option for me .我需要每隔几分钟在 Elastic 中更新数千个项目,不幸的是重新索引对我来说不是一个选项 From my research the best way to update an item is using _update_by_query - I have had success updating single documents like so -根据我的研究,更新项目的最佳方法是使用_update_by_query - 我已经成功更新了单个文档,如下所示 -

{
  "query": {
    "match": {
      "itemId": {
        "query": "1"
      }
    }
  },
  "script": {
    "source": "ctx._source.field = params.updateValue",
    "lang": "painless",
    "params": {
      "updateValue": "test",
    }
  }
}
var response = await Client.UpdateByQueryAsync<dynamic>(q => q
    .Index("masterproducts")
    .Query(q => x.MatchQuery)
    .Script(s => s.Source(x.Script).Lang("painless").Params(x.Params))
    .Conflicts(Elasticsearch.Net.Conflicts.Proceed)
);

Although this works it is extremely inefficient as it generates thousands of requests - is there a way in which I can update multiple documents with a matching ID in a single request?尽管这很有效,但它会生成数千个请求,因此效率极低 - 有没有一种方法可以在单个请求中更新具有匹配 ID 的多个文档? I have already tried Multiple search API which it would seem cannot be used for this purpose.我已经尝试过多重搜索 API似乎不能用于此目的。 Any help would be appreciated!任何帮助,将不胜感激!

If possible, try to generalize your query.如果可能,请尝试概括您的查询。

Instead of targeting a single itemId , perhaps try using a terms query :与其定位单个itemId ,不如尝试使用terms查询

{
  "query": {
    "terms": {
      "itemId": [
        "1", "2", ...
      ]
    }
  },
  "script": {
    ...
  }
}

From the looks of it, your (seemingly simplified) script sets the same value, irregardless of the document ID / itemId .从外观上看,您的(看似简化的)脚本设置了相同的值,而与文档 ID / itemId So that's that.就是这样。

If the script does indeed set different values based on the doc IDs / itemIds , you could make the params multi-value:如果脚本确实根据 doc IDs / itemIds设置了不同的值,则可以将params设为多值:

"params": {
 "updateValue1": "test1",
 "updateValue2": "test2",
 ...
}

and then dynamically access them:然后动态访问它们:

...
def value_to_set = params['updateValue' + ctx._source['itemId']];
...

so the target doc is updated with the corresponding value.因此目标文档会使用相应的值进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM