request.doc("{\"aliasName\":\"中国农业发展银行林州市支行444\",\"bankType\":\"ADB\",\"bankTypeName\":\"中国农业发展银行\",\"cityId\":\"410500\",\"cityName\
必看视频!获取2024年最新Java开发全套学习资料 备注Java
“:“安阳市”,“createTime”:1515719190000,“createUser”:“system”,“id”:“000238a326b044e9ae10cfe4298f4c44”,“isEnabled”:“1”,“name”:“中国农业发展银行林州市支行”,“provinceId”:“410000”,“provinceName”:“河南省”,“unionNumber”:“203496100010”}”, XContentType.JSON);
UpdateResponse resp = highLevelClient.update(request, RequestOptions.DEFAULT);
println(resp.getResult());
}
但是如果不知道文档id的情况如果还想使用UpdateRequest更新文档就需要先使用SearchRequest根据某个条件查询符合条件的文档,然后再循环更新文档即可。
/**
**先检索、再更新文档
**/
@Test
public void search() throws IOException{
SearchRequest searchRequest = new SearchRequest("sub_bank1031");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("_id", "SvjgP24BndtcmnpzbiuL"));
searchSourceBuilder.size(2);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] searchHits = searchResponse.getHits().getHits();
for(SearchHit s:searchHits){
String docId = s.getId();
UpdateRequest request = new UpdateRequest("sub_bank1031","sub_bank",docId);
request.doc("{\"aliasName\":\"中国农业发展银行林州市支行444\",\"bankType\":\"ADB\",\"bankTypeName\":\"中国农业发展银行\",\"cityId\":\"410500\",\"cityName\":\"安阳市\",\"createTime\":1515719190000,\"createUser\":\"system\",\"id\":\"000238a326b044e9ae10cfe4298f4c44\",\"isEnabled\":\"1\",\"name\":\"中国农业发展银行林州市支行\",\"provinceId\":\"410000\",\"provinceName\":\"河南省\",\"unionNumber\":\"203496100010\"}", XContentType.JSON);
UpdateResponse resp = highLevelClient.update(request, RequestOptions.DEFAULT);
println(resp.getResult());
}
}
上面操作略显麻烦,需要多条http请求才能完成,要更新的文档数量很多时将大大降低系统响应速度,这时候我们可以使用es的UpdateByQueryRequest来实现该功能。
/**
* 通过脚本更新文档
* @throws IOException
*/
@Test
public void updateByQueryRequest() throws IOException {
UpdateByQueryRequest request = new UpdateByQueryRequest("sub_bank1031");
request.setDocTypes("sub_bank");
request.setQuery(new TermQueryBuilder("cityId", "511000"));
request.setSize(2);
request.setScript(
new Script(
ScriptType.INLINE, "painless",
"if (ctx._source.bankType == 'BOC') {ctx._source.aliasName='hello'}",
Collections.emptyMap()));
BulkByScrollResponse resp = highLevelClient.updateByQuery(request, RequestOptions.DEFAULT);
}
PS:
pom文件es相关依赖如下:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.0</version>
### 最后
**我还通过一些渠道整理了一些大厂真实面试主要有:蚂蚁金服、拼多多、阿里云、百度、唯品会、携程、丰巢科技、乐信、软通动力、OPPO、银盛支付、中国平安等初,中级,高级Java面试题集合,附带超详细答案,希望能帮助到大家。**
![新鲜出炉的蚂蚁金服面经,熬夜整理出来的答案,已有千人收藏](https://img-blog.csdnimg.cn/img_convert/cf73ea570ea96cd33a7fac5375eaf92f.webp?x-oss-process=image/format,png)
**还有专门针对JVM、SPringBoot、SpringCloud、数据库、Linux、缓存、消息中间件、源码等相关面试题。**
![新鲜出炉的蚂蚁金服面经,熬夜整理出来的答案,已有千人收藏](https://img-blog.csdnimg.cn/img_convert/5f536ef913d7349380e80a4a3816e571.webp?x-oss-process=image/format,png)
l8YtY4-1716409635002)]
**还有专门针对JVM、SPringBoot、SpringCloud、数据库、Linux、缓存、消息中间件、源码等相关面试题。**
[外链图片转存中...(img-oOuCu9u6-1716409635002)]