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": {}
  }
}
相关推荐
TDengine (老段)21 小时前
TDengine 数学函数 DEGRESS 用户手册
大数据·数据库·sql·物联网·时序数据库·iot·tdengine
TDengine (老段)21 小时前
TDengine 数学函数 GREATEST 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
失散1321 小时前
分布式专题——47 ElasticSearch搜索相关性详解
java·分布式·elasticsearch·架构
字节数据平台1 天前
火山引擎Data Agent再拓新场景,重磅推出用户研究Agent
大数据·人工智能·火山引擎
铭毅天下1 天前
Elasticsearch 到 Easysearch 数据迁移 5 种方案选型实战总结
大数据·elasticsearch·搜索引擎·全文检索
跨境小新1 天前
Facebook广告投放:地域定向流量不精准?x个优化指南
大数据·facebook
ZKNOW甄知科技1 天前
客户案例 | 派克新材x甄知科技,构建全场景智能IT运维体系
大数据·运维·人工智能·科技·低代码·微服务·制造
币须赢1 天前
688758赛分科技 阴上阴形态 洗盘上涨?
大数据
学掌门1 天前
大数据知识合集之预处理方法
大数据
Elastic 中国社区官方博客1 天前
Elasticsearch 推理 API 增加了开放的可定制服务
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索