使用elasticsearch 1.7.5更新多个文档

时间:2016-06-01 08:37:42

标签: elasticsearch

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

由于

1 个答案:

答案 0 :(得分:4)

通常,您可以使用Elasticsearch&#39; Bulk API更新多个文档(请参阅1.7。版本文档)。

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

正常更新:

curl -XPOST 'localhost:9200/index1/type1/id1/_update' -d '{
    "name" : "updated John"
}

批量更新:

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"} }

btw:如果您对许多文档进行操作,则最好使用批量请求。这里是详细描述它的docu

编辑: 要更新与查询匹配的某些文档,您可以使用ElasticSearch Update By Query Plugin for ES 1.7。 顺便说一句:对于较新的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\";"
}'

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

script.disable_dynamic: false
script.inline: on 
script.indexed: on
相关问题