ES DSL 常用修改语句

字段值替换修改

修改sql

复制代码
update zyzkwjj set dh=replace(dh,"WS","WSS") where dh like '%WS%'

update zyzkwjj
set 
    dh = replace(dh, 'WS', 'DZ'),
    ztm = replace(ztm, 'WS', 'DZ'),
    zrz = replace(zrz, 'WS', 'DZ')
where 
    dh like '%WS%' 
    or ztm like '%WS%' 
    or zrz like '%WS%';

对应DSL语句

复制代码
POST zyzkwjj/_update_by_query
{
  "script": {
    "source": "ctx._source.dh = ctx._source.dh.replace('WS', 'WSS')",
    "lang": "painless"
  },
  "query": {
    "wildcard": {
      "dh": {
        "value": "*WS*"
      }
    }
  }
}

POST zyzkwjj/_update_by_query
{
  "script": {
    "source": """
      if (ctx._source.dh != null) {
        ctx._source.dh = ctx._source.dh.replace('WS', 'DZ');
      }
      if (ctx._source.ztm != null) {
        ctx._source.ztm = ctx._source.ztm.replace('WS', 'DZ');
      }
      if (ctx._source.zrz != null) {
        ctx._source.zrz = ctx._source.zrz.replace('WS', 'DZ');
      }
    """,
    "lang": "painless"
  },
  "query": {
    "bool": {
      "should": [
        {
          "wildcard": {
            "dh": {
              "value": "*WS*"
            }
          }
        },
        {
          "wildcard": {
            "ztm": {
              "value": "*WS*"
            }
          }
        },
        {
          "wildcard": {
            "zrz": {
              "value": "*WS*"
            }
          }
        }
      ]
    }
  }
}

字段值组合修改

修改SQL

复制代码
update zyzkwjj set dh=concat(qzh,'-',daml,'-',lpad(nd,4,0),'-',bgqx,'-',lpad(jd,4,'0'))  where dh is null or length(dh)<=0

DSL语句

复制代码
POST zyzkwjj/_update_by_query
{
  "script": {
    "source": """
      if (ctx._source.dh == null || ctx._source.dh.length() <= 0) {
        String nd = ctx._source.nd != null ? ctx._source.nd : '';
        String jd = ctx._source.jd != null ? ctx._source.jd : '';
        
     
        String formattedNd = nd.length() < 4 ? String.format('%04d', Integer.parseInt(nd)) : nd;
        String formattedJd = jd.length() < 4 ? String.format('%04d', Integer.parseInt(jd)) : jd;
        
        ctx._source.dh = ctx._source.qzh + '-' + 
                        ctx._source.daml + '-' + 
                        formattedNd + '-' + 
                        ctx._source.bgqx + '-' + 
                        formattedJd;
      }
    """,
    "lang": "painless"
  },
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must_not": {
                    "exists": {
                      "field": "dh"
                    }
                  }
                }
              },
              {
                "script": {
                  "script": "doc['dh'].length() <= 0"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

根据ID修改值

修改SQL

复制代码
update zyzkwjj set create_time=now(),admin_id='111' where id='1111'

DSL语句

复制代码
POST zyzkwjj/_update/1111
{
  "doc": {
    "create_time": "now",
    "admin_id": "111"
  }
}

不加条件修改

复制代码
POST zyzkwjj/_update_by_query
{
  "script": {
    "source": """
      ctx._source.create_time = 'now';
      ctx._source.admin_id = '111';
    """,
    "lang": "painless"
  },
  "query": {
    "match_all": {}
  }
}
相关推荐
Hello.Reader7 小时前
Elasticsearch Node.js 客户端连接指南(Connecting)
elasticsearch·node.js·jenkins
时序数据说8 小时前
时序数据库为什么选IoTDB?
大数据·数据库·物联网·开源·时序数据库·iotdb
Hello.Reader9 小时前
Elasticsearch JS 客户端子客户端(Child Client)实践指南
大数据·javascript·elasticsearch
阑梦清川11 小时前
派聪明RAG知识库----关于elasticsearch报错,重置密码的解决方案
大数据·elasticsearch·jenkins
ID_1800790547312 小时前
淘宝拍立淘按图搜索API接口功能详细说明
大数据·python·json·图搜索算法
Hello.Reader12 小时前
Elasticsearch Node.js 客户端的安装
elasticsearch·node.js·vim
我要学习别拦我~13 小时前
读《精益数据分析》:媒体内容平台全链路梳理
大数据·数据分析·媒体
六哥探店实录14 小时前
外卖:重构餐饮的线上服务密码
大数据·生活·美食
计算机毕设-小月哥16 小时前
【限时分享:Hadoop+Spark+Vue技术栈电信客服数据分析系统完整实现方案
大数据·vue.js·hadoop·python·信息可视化·spark·计算机毕业设计
tonydf16 小时前
ELK开启安全策略
大数据·后端·安全