zoukankan      html  css  js  c++  java
  • REST API (更新文档)

    Elasticsearch的更新文档API准许通过脚本操作来更新文档。更新操作从索引中获取文档,执行脚本,然后获得返回结果。它使用版本号来控制文档获取或者重建索引。

    我们新建一个文档:

    请求:PUT http://localhost:9200/test/type1/1?pretty

    参数:

    {
        "counter" : 1,
        "tags" : ["red"]
    }

    脚本开启功能

        在最新版本的Elasticsearch中,基于安全考虑(如果用不到,请保持禁用),默认禁用了动态脚本功能.如果被禁用,在使用脚本的时候则报以下的错误:

    scripts of type [inline], operation [update] and lang [groovy] are disabled

    可以用以下方式完全开启动态脚本功能,在config/elasticsearch.yml文件,在最后添加以下代码:

    script.inline: on
    script.indexed: on
    script.file: on

    配置后,重启Elasticsearch。

    下面我们用脚本来更新此文档。

    请求:POST http://localhost:9200/test/type1/1/_update?pretty

    参数:

    {
        "script" : {
            "inline": "ctx._source.counter += count",
            "params" : {
                "count" : 4
            }
        }
    }

    执行完后,我们在查询一下文档内容,可以发现counter的值为5:

    {
      "_index" : "test",
      "_type" : "type1",
      "_id" : "1",
      "_version" : 5,
      "found" : true,
      "_source" : {
        "counter" : 5,
        "tags" : [ "red" ]
      }
    }

    在看下面的更新操作:

    请求:POST http://localhost:9200/test/type1/1/_update?pretty

    参数:

    {
        "script" : {
            "inline": "ctx._source.tags += tag",
            "params" : {
                "tag" : "blue"
            }
        }
    }

    返回的内容为,表示更新成功,我们看一下_version为6,比刚才的值增加了1:

    {
      "_index" : "test",
      "_type" : "type1",
      "_id" : "1",
      "_version" : 6,
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      }
    }

    然后我们在查询一下文档内容:

    {
      "_index" : "test",
      "_type" : "type1",
      "_id" : "1",
      "_version" : 6,
      "found" : true,
      "_source" : {
        "counter" : 5,
        "tags" : [ "red", "blue" ]
      }
    }

    在脚本中除了_source外其他内置参数也可以使用,例如_index, _type, _id, _version, _routing, _parent, _timestamp, _ttl。

    下面我们通过脚本增加一列。

    请求:POST http://localhost:9200/test/type1/1/_update?pretty

    参数:

    {
        "script" : "ctx._source.name_of_new_field = "value_of_new_field""
    }

    然后查询此文档:

    {
      "_index" : "test",
      "_type" : "type1",
      "_id" : "1",
      "_version" : 7,
      "found" : true,
      "_source" : {
        "counter" : 5,
        "tags" : [ "red", "blue" ],
        "name_of_new_field" : "value_of_new_field"
      }
    }

    从中可以看出,文档中又增加了一列。

    删除一列,请求和刚才的一样,参数变为:

    {
        "script" : "ctx._source.remove("name_of_new_field")"
    }

    甚至可以通过表达式来判断做某些事情,例如:下面的示例将删除的文件如果标签字段包含蓝色,否则什么也不做(空):

    请求参数:

    {
        "script" : {
            "inline": "ctx._source.tags.contains(tag) ? ctx.op = "delete" : ctx.op = "none"",
            "params" : {
                "tag" : "blue"
            }
        }
    }

    部分文档更新:

        该更新接口还支持更新部分文档,将文档合并到现有文档中(简单的递归合并、对象的内部合并、替换核心的“键/值”和数组)。例如:

    {
        "doc" : {
            "name" : "new_name"
        }
    }

    更新后,可以发现文档中多了一列name。

    {
      "_index" : "test",
      "_type" : "type1",
      "_id" : "1",
      "_version" : 23,
      "found" : true,
      "_source" : {
        "counter" : 5,
        "tags" : [ "red", "blue" ],
        "name" : "new_name"
      }
    }

    当文档指定的值与现有的_source合并。当新的文档和老的文档不一致的时候,文档将会被从新建立索引。当新旧文档一样的时候,则不进行从建索引的操作。可以通过设置detect_noop为false,让任何情况下都从新建立索引,例如下面的更新操作:

    {
        "doc" : {
            "name" : "new_name"
        },
        "detect_noop": false
    }
  • 相关阅读:
    学习认识Spring原理
    JavaScript
    JSP----九大内置对象
    人到底可以有怎样的意志力,人到底可能有怎样的发挥潜力? ----问自己
    将web项目打成war包部署在tomcat步骤
    微信企业公众号开发之回调模式
    eclipse/myeclipse 中的一些常用的快捷键
    mycelipse中关于编码的配置
    Ext学习系列(1)初识Ext
    AngularJs编写指令
  • 原文地址:https://www.cnblogs.com/jiu0821/p/5635610.html
Copyright © 2011-2022 走看看