es groovy脚本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/keyuquan/article/details/82023366

1.数据准备

PUT /test_index/test_type/11
{
  "num": 0,
  "tags": []
}

2.内置脚本实现 partial update

POST /test_index/test_type/11/_update
{
   "script" : "ctx._source.num+=1"
}

3. 外部脚本 实现 partial update

 #脚本路径../elasticsearch-5.2.0/config/scripts/test-add-tags.groovy
 #脚本内容ctx._source.tags+=new_tag

POST /test_index/test_type/11/_update
{
  "script": {
    "lang": "groovy", 
    "file": "test-add-tags",
    "params": {
      "new_tag": "tag1"
    }
  }
}

4.外部脚本实现删除文档

#脚本路径../elasticsearch-5.2.0/config/scripts/test-delete-document.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
    }
  }
}

5.内部脚本实现 upsert操作

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

POST /test_index/test_type/11/_update
{
   "script" : "ctx._source.num+=1",
   "upsert": {
       "num": 0,
       "tags": []
   }
}

猜你喜欢

转载自blog.csdn.net/keyuquan/article/details/82023366