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": {}
  }
}
相关推荐
Lx3522 小时前
Hadoop容错机制深度解析:保障作业稳定运行
大数据·hadoop
muyun28005 小时前
Docker 下部署 Elasticsearch 8 并集成 Kibana 和 IK 分词器
elasticsearch·docker·容器
T06205146 小时前
工具变量-5G试点城市DID数据(2014-2025年
大数据
向往鹰的翱翔7 小时前
BKY莱德因:5大黑科技逆转时光
大数据·人工智能·科技·生活·健康医疗
鸿乃江边鸟8 小时前
向量化和列式存储
大数据·sql·向量化
IT毕设梦工厂9 小时前
大数据毕业设计选题推荐-基于大数据的客户购物订单数据分析与可视化系统-Hadoop-Spark-数据可视化-BigData
大数据·hadoop·数据分析·spark·毕业设计·源码·bigdata
java水泥工9 小时前
基于Echarts+HTML5可视化数据大屏展示-白茶大数据溯源平台V2
大数据·echarts·html5
广州腾科助你拿下华为认证11 小时前
华为考试:HCIE数通考试难度分析
大数据·华为
在未来等你13 小时前
Elasticsearch面试精讲 Day 17:查询性能调优实践
大数据·分布式·elasticsearch·搜索引擎·面试
大数据CLUB16 小时前
基于spark的澳洲光伏发电站选址预测
大数据·hadoop·分布式·数据分析·spark·数据开发