【问题标题】:Elastic Search update fields in the document文档中的 Elastic Search 更新字段
【发布时间】:2018-08-01 04:43:45
【问题描述】:

在 ES 中,需要更新文档中的现有字段。我可以轻松地在文档中添加新字段。

POST {"doc":{"newFields":"value"}} 

- 它将新字段添加到现有文档中。但我无法添加此场景。

{"name":"aa","otherinfo": {"hobbies":["cricket","football"]} }
  1. 在这里我需要添加另一个字段 eduction 字段 - 我该怎么做?
  2. 假设我需要将篮球添加到现有的爱好中
  3. 从爱好中删除所有字段并仅添加投掷球

有什么帮助吗?

【问题讨论】:

    标签: hadoop elasticsearch elasticsearch-plugin


    【解决方案1】:

    请查看Update API

    你可以update with a partial document。示例:

    POST test/_doc/1/_update
    {
      "doc" : {
        "name" : "new_name"
      }
    }
    

    但此外,您可以使用scripting 更新文档。例子:

    我们可以在标签列表中添加一个标签(注意,如果标签存在,它仍然会添加它,因为它是一个列表):

    POST test/_doc/1/_update
    {
      "script" : {
        "source": "ctx._source.tags.add(params.tag)",
        "lang": "painless",
        "params" : {
          "tag" : "blue"
        }
      }
    }
    

    我们还可以在文档中添加一个新字段:

    POST test/_doc/1/_update
    {
      "script" : "ctx._source.new_field = 'value_of_new_field'"
    }
    

    或者从文档中删除一个字段:

    POST test/_doc/1/_update
    {
      "script" : "ctx._source.remove('new_field')"
    }
    

    还有更多。

    【讨论】:

      猜你喜欢
      • 2018-08-02
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多