zoukankan      html  css  js  c++  java
  • (37)ElasticSearch基于groovy脚本执行partial update

      1、准备数据

    PUT /lib/user/1
    {
        "first_name":"Jane",
        "last_name":"Smith",
        "age":32,
        "about":"I like to collect rock albums",
        "interests":[ "music" ]
    }

      2、操作演示

      (1)age增加1

    GET /lib/user/1/_update
    {
      "script":"ctx._source.age+=1"
    }

      查看结果:GET /lib/user/1

    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "first_name": "Jane",
        "last_name": "Smith",
        "age": 33,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    }

      (2)改变last_name的值

    GET /lib/user/1/_update
    {
      "script":"ctx._source.last_name+='hehe'"
    }

      查看结果:GET /lib/user/1

    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 3,
      "found": true,
      "_source": {
        "first_name": "Jane",
        "last_name": "Smithhehe",
        "age": 33,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    }

      3)增加一个interests

    GET /lib/user/1/_update
    {
      "script":{
      "source":"ctx._source.interests.add(params.tag)",
      "params":{
        "tag":"football"
      }
    }
    }

      查看结果:GET /lib/user/1

    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 4,
      "found": true,
      "_source": {
        "first_name": "Jane",
        "last_name": "Smithhehe",
        "age": 33,
        "about": "I like to collect rock albums",
        "interests": [
          "music",
          "football"
        ]
      }
    }

      4)删除一个interests

    GET /lib/user/1/_update
    {
      "script":{
        "source":"ctx._source.interests.remove(ctx._source.interests.indexOf(params.tag))",
        "params":{
           "tag":"football"
         }
      }
    }

      查看结果:GET /lib/user/1

    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 5,
      "found": true,
      "_source": {
        "first_name": "Jane",
        "last_name": "Smithhehe",
        "age": 33,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    }

      5)删除该文档,删除年龄为33的文档

    GET /lib/user/1/_update
    {
      "script":{
        "source":"ctx.op=ctx._source.age==params.count?'delete':'none'",
        "params":{
          "count":33
        }
      }
    }

      查看结果:GET /lib/user/1

    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "found": false
    }

      6)有该文档的话给age增加1,没有添加一个文档

    GET /lib/user/1/_update
    {
      "script":"ctx._source.age+=1",
      "upsert":{
         "first_name": "Jane",
        "last_name": "Smith",
        "age": 18,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    }

      查看结果:GET /lib/user/1

    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "first_name": "Jane",
        "last_name": "Smith",
        "age": 18,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    }
  • 相关阅读:
    第二题:坦克游戏1.0(方法:动态规划)
    第一题:小鼠迷宫问题(方法:广搜)
    我的世界之电脑mod小乌龟 —— 方位上的操作 lua函数集
    NOIP 2011 提高组 选择客栈(vijos 1737)(方法:队列,数学)
    codeforces_1040_A Python练习
    codeforces_466_C Python练习
    codeforces_158_B Python练习
    三.Python_scrapy的Item对象 学习笔记
    二.Pyhon_scrapy终端(scrapy shell)学习笔记
    一.Python_srcrapy的命令行工具 学习笔记(Command line tool)
  • 原文地址:https://www.cnblogs.com/javasl/p/12643716.html
Copyright © 2011-2022 走看看