{
“script”: {
“source”: “ctx._source[‘要修改的字段名’]=‘要修改为的参数’”
},
“query”: {
“term”: {
“条件字段名”: “条件字段参数”
}
}
}
POST 索引名/_update_by_query
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "url"
}
},
],
"must_not": [
{
"exists": {
"field": "newUrl"
}
}
]
}
},
"script":{
"inline" : "ctx._source.newUrl= ctx._source.url;",
"lang" : "painless"
}
}
POST 索引名/_update_by_query
{
"query": {
"match_all": {}
},
"script":{
"source" : "ctx._source.newUrl= params.url",
"params": {
"url": "www.baidu.com"
}
}
}
GET sourcedoc_prod/_update_by_query
{
"query": {
"exists": {
"field": "linked_persons"
}
},
"script": {
"source": " if (ctx._source.containsKey('linked_persons')) {
if(!ctx._source.linked_persons.contains(params.linked_persons)){
ctx._source.linked_persons.add(params.linked_persons);
}
}else {
ctx._source.linked_persons= [params.linked_persons];
}",
"params": {
"linked_persons": "111"
}
}
}
@Test
public void testUpdateByQuery() throws IOException {
Set<String> docIds = new HashSet<>();
docIds.add("xxxxxxxxxxx");
HashMap<String, Object> params = new HashMap<>(2);
params.put("linked_persons","张三");
Script script = new Script(ScriptType.INLINE, "painless",
"if(ctx._source.containsKey('linked_persons')){ if (!ctx._source.linked_persons.contains(params.linked_persons)){ ctx._source.linked_persons.add(param s.linked_persons); }} else { ctx._source.linked_persons = [params.linked_persons];}",
params);
// 构造查询条件,查询需要进行修改的文档
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.must(QueryBuilders.termsQuery("id",docIds));
updateByQuery(queryBuilder , script , Index_a, Index_b);
}
/**
* 通过脚本 批量修改 文档es
* @param queryBuilder 查询条件
* @param script 脚本
* @param indices 需要修改的 索引
* @throws IOException
*/
public void updateByQuery(QueryBuilder queryBuilder, Script script, String... indices) throws IOException {
UpdateByQueryRequest request = new UpdateByQueryRequest(indices);
request.setConflicts("proceed");
request.setQuery(queryBuilder);
request.setScript(script);
restHighLevelClient.updateByQuery(request, RequestOptions.DEFAULT);
}
免责申明:
本文系转载,版权归原作者所有,如若侵权请联系我们进行删除!
《数据治理行业实践白皮书》下载地址:https://fs80.cn/4w2atu
《数栈V6.0产品白皮书》下载地址:https://fs80.cn/cw0iw1
想了解或咨询更多有关袋鼠云大数据产品、行业解决方案、客户案例的朋友,浏览袋鼠云官网:https://www.dtstack.com/?src=bbs
同时,欢迎对大数据开源项目有兴趣的同学加入「袋鼠云开源框架钉钉技术群」,交流最新开源技术信息,群号码:30537511,项目地址:https://github.com/DTStack