zoukankan      html  css  js  c++  java
  • ElasticSearch6.3脚本更新

    使用上篇文章创建的索引进行学习: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"
        }
      }
    }

  • 相关阅读:
    初始化生成linux sysfs(8)
    内存延迟监控系统组件
    数组代码First Missing Positive
    类文件Spring中空值的写法java教程
    状态键盘完美适应iOS中的键盘高度变化
    框架绑定JavaScript MVC框架PK:Angular、Backbone、CanJS与Ember
    域编码jquery的AJAX跨域请求及跨域请求的原理
    数据格式利用GSON接卸JSON数据
    网元查看一个无厘头的core dump问题定位
    类型应用oracle如何显示毫秒?
  • 原文地址:https://www.cnblogs.com/wangymd/p/11244856.html
Copyright © 2011-2022 走看看