(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"
    ]
  }
}

原文地址:https://www.cnblogs.com/javasl/p/12643716.html

时间: 2024-08-28 01:31:55

(37)ElasticSearch基于groovy脚本执行partial update的相关文章

Elasticsearch 顶尖高手(19)—基于groovy脚本执行partial update

es,其实是有内置脚本支持的, 可以基于groovy脚本实现各种各样的复杂操作 基于groovy脚本,如何执行partial update 创建数据 PUT /test_index/test_type/11 {   "num":0,   "tags":[] } 1.内置脚本 POST /test_index/test_type/11/_update { "script":"cts._source.num+=1" } 2.外部脚

soapui-使用groovy脚本执行用例请求

import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext CURRENT_TESTCASE = testRunner.testCase TEST_SUITE = CURRENT_TESTCASE.parent def testStep = TEST_SUITE.getTestCaseByName('TestCase').getTestStepByName('Login') def testStepContext = new W

Elasticsearch技术解析与实战(七)Elasticsearch partial update

普通的partial update 1.插入测试数据 PUT /test_index/test_type/10 { "test_field1": "test1", "test_field2": "test2" } 2.更新 POST /test_index/test_type/10/_update { "doc": { "test_field2": "updated test2

Elasticsearch学习笔记(九)partial update

一.什么是partial update? PUT /index/type/id,创建文档&替换文档,就是一样的语法 一般对应到应用程序中,每次的执行流程基本是这样的: (1)应用程序先发起一个get请求,获取到document,展示到前台界面,供用户查看和修改 (2)用户在前台界面修改数据,发送到后台 (3)后台代码,会将用户修改的数据在内存中进行执行,然后封装好修改后的全量数据 (4)然后发送PUT请求,到es中,进行全量替换 (5)es将老的document标记为deleted,然后重新创建

Elasticsearch系统学习(八)-partial update

一.partial update介绍 1.1.什么是partial update? 1)PUT /index/type/id 创建文档&替换文档,是一样的语法.一般对应到应用程序中,每次的执行流程基本是这样的: (1)应用程序先发起一个get请求,获取到document,展示到前台界面,供用户查看和修改 (2)用户在前台界面修改数据,发送到后台 (3)后台代码,会将用户修改的数据在内存中进行执行,然后封装好修改后的全量数据 (4)然后发送PUT请求,到es中,进行全量替换 (5)es将老的doc

Elasticsearch的Groovy Script自定义评分检索

需求:以索引中的boostapp列作为评分的基础分值,同时根据carpublishtime(数据的刷新时间字段)按时间进行衰减. 基于Groovy脚本实现. 1.query脚本方式: { "fields": [ "boost", "ucarid", "boostapp", "carpublishtime" ], "query": { "function_score":

ElasticSearch Groovy脚本远程代码执行漏洞

什么是ElasticSearch? 它是一种分布式的.实时性的.由JAVA开发的搜索和分析引擎. 2014年,曾经被曝出过一个远程代码执行漏洞(CVE-2014-3120),漏洞出现在脚本查询模块,由于搜索引擎支持使用脚本代码(MVEL),作为表达式进行数据操作,攻击者可以通过MVEL构造执行任意java代码,后来脚本语言引擎换成了Groovy,并且加入了沙盒进行控制,危险的代码会被拦截,结果这次由于沙盒限制的不严格,导致远程代码执行任意命令..."任意"你懂的,比如:利用nc反弹sh

【java web】java执行预编译Groovy脚本

在JVM中运行Groovy类有两种方式: 使用Groovy编译所有的*.groovy为java的*.class文件,把这些*.class文件放在java类路径中,通过java类加载器来加载这些类. 通过groovy类加载器在运行时直接加载*.groovy文件并生成对象.在这种方式下,没有生成任何*.class,但是生成了一个java.lang.Class对象的实例. 下面介绍前一种使用Groovy的方法:编译成java字节码并且作为正常java应用程序运行在java虚拟机上,即预编译模式. 1.

Java执行groovy脚本

1 Binding binding = new Binding(); 2 binding.setVariable("foo", new Integer(2)); 3 GroovyShell shell = new GroovyShell(binding); 4 5 String script = "import com.myb.to.infrastructure.Md5Util; " 6 + "def a = 12; println 'C# md5:' +