使用上篇文章创建的索引进行学习:https://www.cnblogs.com/wangymd/p/11200996.html

官方文档:https://www.elastic.co/guide/en/elasticsearch/painless/6.3/painless-examples.html

1、脚本更新指定字段

方式1:

POST test_index/test_type/4/_update
{
  "script":{
    "source":"ctx._source.count = 10"
  }
}

方式2:

POST test_index/test_type/4/_update
{
  "script":{
    "source":"ctx._source.count = params.count",
    "params": {
      "count":20
    }
  }
}

POST test_index/test_type/4/_update

{
  "script" : {
    "source": "ctx._source.count++"  #自增
  }
}

POST test_index/test_type/4/_update
{
  "script" : {
    "source": "ctx._source.count--"  #自减
  }
}

2、数组添加值

索引增加字段

PUT test_index/test_type/_mapping
{
  "properties": {
    "tags" : {
        "type": "text"
    }
  }
}

索引字段设置数组值

POST test_index/test_type/4/_update
{
  "doc": {
    "tags":["aa"]
  }
}

索引字段添加数组值

注意字段无数据时直接添加会发生错误。

POST test_index/test_type/4/_update
{
  "script":{
    "source":"ctx._source.tags.add(params.tags)",
    "params": {
      "tags":"bb"
    }
  }
}

3、添加字段

POST test_index/test_type/4/_update
{
  "script" : "ctx._source.date = '2019-07-25'"  #字段名和字段值
}

4、删除字段

POST test_index/test_type/4/_update
{
  "script" : "ctx._source.remove('date')"
}

5、复杂的脚本

①根据不同条件执行不同的命令

POST test_index/test_type/4/_update
{
  "script" : {
    "source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }",  #tags包含aa"进行删除",其他误操作
    "lang": "painless",
    "params" : {
      "tag" : "aa"
    }
  }
}

最新文章

  1. PHP中的header()函数作用
  2. php多进程刷票
  3. Java知识总结--数据库
  4. Yii 生成表单下拉选框及查询下拉选框
  5. abstract修饰符,具体类与抽象类的区别
  6. .NET PageAdmin CMS
  7. Hack写法
  8. vim的复制粘贴小结(转)
  9. UESTC 1222 Sudoku
  10. jsp 文件使用 include指令 导入 jspf 分析,及导入jspf 文件后出现乱码问题
  11. Simple prefix compression
  12. (PAT)L2-004 这是二叉搜索树吗?(数据结构)
  13. Geometric regularity criterion for NSE: the cross product of velocity and vorticity 4: $u\cdot \om$
  14. Win10 自定义右键新建菜单
  15. day05函数和模块
  16. 安全易用的云许可-VirboxLM许可管理平台
  17. Java中的标签
  18. linux上安装python2.7.11
  19. ActiveMq 配置多队列
  20. 什么是虚拟DOM?

热门文章

  1. 从X86指令深扒JVM的位移操作
  2. 抛弃os.path,拥抱pathlib
  3. 用JetBrains PyCharm 开发工具写一个简单python案例
  4. LAMP搭建wordpress
  5. python中的基础坑
  6. [转载] Objectiv-C 入门一二三
  7. redis学习——day01_redis简介与安装
  8. vscode格式化Vue出现的问题:单引号变双引号 格式化去掉分号
  9. 去掉shiro登录时url里的JSESSIONID https://blog.csdn.net/aofavx/article/details/51701012
  10. 四使用浮动div布局