es修改搜索结果的指定字段
程序员文章站
2022-07-14 22:30:30
...
1、原生语句
POST /infomations/infomations/_update_by_query
JSON请求格式
{
"query": {
"match": {
"status": "UP_SHELF"
}
},
"script": {
"inline": "ctx._source['status'] = 'DOWN_SHELF'"
}
}
假如是修改多个:多个的话就用分号隔开。
ctx._source[字段名] = “值”;ctx._source[字段名] = “值”;
2.java操作
Client client = elasticsearchTemplate.getClient();
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
String name = "修改数值";
updateByQuery.source("索引")
//查询要修改的结果集
.filter(QueryBuilders.termQuery("field", 412))
//修改操作
.script(new Script( "ctx._source['field']='"+ name+"';ctx._source['field']='"+name+"'"));
//响应结果集
BulkByScrollResponse response = updateByQuery.get();
long updated = response.getUpdated();