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": {}
  }
}
相关推荐
浪子小院2 小时前
ModelEngine 智能体全流程开发实战:从 0 到 1 搭建多协作办公助手
大数据·人工智能
闲人编程2 小时前
Elasticsearch搜索引擎集成指南
python·elasticsearch·搜索引擎·jenkins·索引·副本·分片
AEIC学术交流中心2 小时前
【快速EI检索 | ACM出版】2026年大数据与智能制造国际学术会议(BDIM 2026)
大数据·制造
wending-Y2 小时前
记录一次排查Flink一直重启的问题
大数据·flink
UI设计兰亭妙微2 小时前
医疗大数据平台电子病例界面设计
大数据·界面设计
先跑起来再说3 小时前
Git 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea
初恋叫萱萱3 小时前
模型瘦身实战:用 `cann-model-compression-toolkit` 实现高效 INT8 量化
大数据
互联网科技看点3 小时前
孕期科学补铁,保障母婴健康-仁合益康蛋白琥珀酸铁口服溶液成为产妇优选方案
大数据
Dxy12393102164 小时前
深度解析 Elasticsearch:从倒排索引到 DSL 查询的实战突围
大数据·elasticsearch·搜索引擎
YongCheng_Liang4 小时前
零基础学大数据:大数据基础与前置技术夯实
大数据·big data