ES DSL 常用修改语句

发布于:2025-04-17 ⋅ 阅读:(29) ⋅ 点赞:(0)

字段值替换修改

修改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": {}
  }
}


网站公告

今日签到

点亮在社区的每一天
去签到