如果更新一条文档,而且知道文档id的前提下可以使用UpdateRequest即可实现,代码如下:
代码语言:javascript复制 /**
* 根据文档id更新
* @throws IOException
*/
@Test
public void test() throws IOException {
UpdateRequest request = new UpdateRequest("sub_bank1031","sub_bank","SvjgP24BndtcmnpzbiuL");
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());
}
但是如果不知道文档id的情况如果还想使用UpdateRequest更新文档就需要先使用SearchRequest根据某个条件查询符合条件的文档,然后再循环更新文档即可。
代码语言:javascript复制 /**
**先检索、再更新文档
**/
@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来实现该功能。
代码语言:javascript复制 /**
* 通过脚本更新文档
* @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相关依赖如下:
代码语言:javascript复制 <dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.0</version>
</dependency>
<!-- elasticsearch high level -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
<version>6.8.0</version>
</dependency>