美文网首页
20、分布式文档系统_Elasticsearch动手实战演练基于

20、分布式文档系统_Elasticsearch动手实战演练基于

作者: 拉提娜的爸爸 | 来源:发表于2019-12-31 16:36 被阅读0次

    es有个内置的脚本支持的,可以基于groovy脚本实现各种各样的复杂操作。
    基于groovy脚本,如何执行partial update
    es scripting module初步了解
    首先构造一个document数据

    PUT /test_index/test_type/11
    {
      "num":0,
      "tags":[]
    }
    -----------------------------------结果-----------------------------------
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "11",
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      },
      "created": true
    }
    

    1、内置脚本

    POST /test_index/test_type/11/_update
    {
      "script": "ctx._source.num+=1"
    }
    -----------------------------------查看结果-----------------------------------
    GET test_index/test_type/11
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "11",
      "_version": 2,
      "found": true,
      "_source": {
        "num": 1,
        "tags": []
      }
    }
    

    2、外部脚本

    Elasticsearch\elasticsearch-5.2.0\config\scripts目录下创建groovy脚本
    例如:创建名为test-add-tags.groovy的文件,文件脚本为给tags添加新内容ctx._source.tags+=new_tag

    如图所示在\config\scripts文件夹下创建test-add-tags.groovy脚本
    脚本内容
    运行脚本,lang指定脚本后缀类型,file指定脚本文件名,params指定参数
    POST test_index/test_type/11/_update
    {
      "script": {
        "lang": "groovy",
        "file": "test-add-tags",
        "params": {
          "new_tag":"tag1"
        }
      }
    }
    -----------------------------------运行结果成功-----------------------------------
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "11",
      "_version": 3,
      "result": "updated",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      }
    }
    -----------------------------------查看数据-----------------------------------
    GET /test_index/test_type/11
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "11",
      "_version": 3,
      "found": true,
      "_source": {
        "num": 1,
        "tags": [
          "tag1"
        ]
      }
    }
    

    3、用脚本删除文档

    步骤同上,创建groovy脚本文件,写入脚本内容:
    ctx.op = ctx._source.num == count ? 'delete' : 'none'

    POST /test_index/test_type/11/_update
    {
      "script": {
        "lang": "groovy",
        "file": "test-delete-document",
        "params": {
          "count":1
        }
      }
    }
    -----------------------------------运行结果成功-----------------------------------
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "11",
      "_version": 4,
      "result": "deleted",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      }
    }
    -----------------------------------查看数据-----------------------------------
    GET /test_index/test_type/11
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "11",
      "found": false
    }
    

    如果document数据以删除标记为deleted,那么对该条数据进行partial update时会报错

    POST test_index/test_type/11/_update
    {
      "doc": {
        "num":1
      }
    }
    -----------------------------------运行结果返回异常-----------------------------------
    {
      "error": {
        "root_cause": [
          {
            "type": "document_missing_exception",
            "reason": "[test_type][11]: document missing",
            "index_uuid": "8GnE3L60TnWxjSrPQ7Xa2A",
            "shard": "0",
            "index": "test_index"
          }
        ],
        "type": "document_missing_exception",
        "reason": "[test_type][11]: document missing",
        "index_uuid": "8GnE3L60TnWxjSrPQ7Xa2A",
        "shard": "0",
        "index": "test_index"
      },
      "status": 404
    }
    

    4、upsert操作

    如果指定的document不存在,就执行upsert中的初始化操作;如果指定的document存在,就执行doc或者script指定的partial update操作

    POST test_index/test_type/11/_update
    {
      "script": "ctx._source.num+=1",
      "upsert": {
        "num":0,
        "tags":[]
      }
    }
    -----------------------------------运行结果返回成功-----------------------------------
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "11",
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      }
    }
    -----------------------------------查看document数据-----------------------------------
    GET /test_index/test_type/11
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "11",
      "_version": 1,
      "found": true,
      "_source": {
        "num": 0,
        "tags": []
      }
    }
    因为document id为11的数据被删除为deleted,所以运行upsert中的初始化操作,最终数据为upsert初始化后的值。
    

    相关文章

      网友评论

          本文标题:20、分布式文档系统_Elasticsearch动手实战演练基于

          本文链接:https://www.haomeiwen.com/subject/jenboctx.html