使用elasticsearch 1.7.5更新多个文档

ove*_*urn 0 elasticsearch

知道如何更新 elasticsearch 1.7 索引上的多个文档吗?我已经看过 2.3 文档,并且可以通过查询 api 调用进行更新,但我无法更新此特定实例,而且我似乎无法在那里找到此功能。

谢谢

Han*_*nsa 5

一般来说,您可以使用Elasticsearch的Bulk API更新多个文档(参考1.7版本的docu)。

在这里您可以看到正常更新(通过curl命令)和批量更新之间的区别。

正常更新:

curl -XPOST 'localhost:9200/index1/type1/id1/_update' -d '{
    "name" : "updated John"
}
Run Code Online (Sandbox Code Playgroud)

批量更新:

POST /_bulk
{ "update": { "_index": "index1", "_type": "type1", "_id": "id1", "_retry_on_conflict" : 3} }
{ "doc" : {"name" : "updated John"} }
{ "update": { "_index": "index1", "_type": "type1", "_id": "id1", "_retry_on_conflict" : 3} }
{ "doc" : {"name" : "The second update for John"} }
Run Code Online (Sandbox Code Playgroud)

顺便说一句:如果您对许多文档进行操作,批量请求是更好的选择。这是详细描述它的文档。


编辑:要更新与查询匹配的某些文档,您可以使用ES 1.7 的ElasticSearch 按查询更新插件。顺便说一句:对于较新的 ES 版本,此功能是内置的:Update By Query API

以下是该插件语法的示例:

curl -XPOST 'localhost:9200/index1/_update_by_query' -d '
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [ 
            {
              "term": {
                "Category": "YourCategoryValueToSearchFor"
              }
            }
          ]
        }
      }
    }
  },
  "script" : "ctx._source.category = \"NewCategoryValue\"; ctx._source.category2 = \"OptionallyUpdateAnotherField\";"
}'
Run Code Online (Sandbox Code Playgroud)

为此,您还必须在 elasticsearch.yml 配置中启用脚本:

script.disable_dynamic: false
script.inline: on 
script.indexed: on
Run Code Online (Sandbox Code Playgroud)