简体   繁体   English

使用update_by_query API更新多个文档?

[英]Update mutiple documents using update_by_query API?

Using update_by_query we can update only existing fields that are present in mappings or we can able to create a new field with a value in all documents.. 使用update_by_query,我们可以仅更新映射中存在的现有字段,也可以创建所有文档中带有值的新字段。

Because i tried the following query for creating a new field in all documents in the index: 因为我尝试使用以下查询在索引中的所有文档中创建新字段:

  {
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "uniqueId": 805569568956
              }
            }
          ]
        }
      }
    }
  },
  "script": {
    "inline": "ctx._source.Univesity.Name.Region.Europe = 'UCLA'"
  }
}

IT throws an error like this IT抛出这样的错误

     {
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "failed to run inline script [ctx._source.Univesity.Name.Region.Europe = 'UCLA'] using lang [groovy]"
      }
    ],
    "type": "script_exception",
    "reason": "failed to run inline script [ctx._source.Univesity.Name.Region.Europe = 'UCLA'] using lang [groovy]",
    "caused_by": {
      "type": "null_pointer_exception",
      "reason": "Cannot get property 'Name' on null object"
    }
  },
  "status": 500
}

In elasticsearch.yaml i configured the following properties: 我在elasticsearch.yaml中配置了以下属性:

script.inline: true
script.indexed: true
script.engine.groovy.inline.aggs:  on
script.engine.groovy.inline:          true

My mapping is something like this: 我的映射是这样的:

"mappings": {
         "Univesity": {
            "properties": {
               "Name": {
                  "properties": {
                     "Region": {
                        "properties": {
                           "Europe": {
                              "type": "string"
                           }
                           "Australia": {
                              "type": "string"
                           }
                        }
                     },

You need to use quotes, otherwise the Elasticsearch engine is going to look for a variable called UCLA. 您需要使用引号,否则,Elasticsearch引擎将查找名为UCLA的变量。 Your inline script should rather look like: 您的内联脚本应该看起来像:

"script": {
    "inline": "ctx._source.University.collegename = 'UCLA'"
}

(Notice the simple quotes around UCLA) (注意有关UCLA的简单引号)

Edit: See a full working query example: 编辑:查看完整的工作查询示例:

POST employeeid/_update_by_query?conflicts=proceed
{
  "query":{
    "match": {
      "name": "Sam"
    }
  },
  "script":{
    "inline": "ctx._source.importance2 = 2000"
  }
}

Also be careful as the doc mentions this update by query is still experimental (see: https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-update-by-query.html ): 还要小心,因为文档提到此查询更新仍处于试验阶段(请参阅: https : //www.elastic.co/guide/zh-CN/elasticsearch/reference/master/docs/update-by-query.html ):

The update-by-query API is new and should still be considered experimental. 按查询更新API是新的,仍应视为实验性的。

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

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