es java 部分更新_Elasticsearch之更新

public classUpdateElasticAPI {private staticRestClient restClient;static{

restClient=RestClient.builder(new HttpHost("localhost",9200,"http")).build();

}/*** 1.创建文档

*@throwsException*/@Testpublic void CreateDocument()throwsException{

String method= "PUT";

String endpoint= "/update_index/test/1";

HttpEntity entity= newNStringEntity("{\n" +

" \"counter\" : 1,\n" +

" \"tags\" : [\"red\"]\n" +

"}", ContentType.APPLICATION_JSON);

Response response= restClient.performRequest(method,endpoint, Collections.emptyMap(),entity);

System.out.println(EntityUtils.toString(response.getEntity()));

}/***2. 获取文档

*@throwsException*/@Testpublic void getDocument()throwsException{

String method= "GET";

String endpoint= "/update_index/test/1";

Response response=restClient.performRequest(method,endpoint);

System.out.println(EntityUtils.toString(response.getEntity()));

}/*** 3.更新文档,给counter字段加4

*@throwsException*/@Testpublic void UpdateDocument()throwsException{

String method= "POST";

String endpoint= "/update_index/test/1/_update";

HttpEntity entity= newNStringEntity("{\n" +

" \"script\" : {\n" +

" \"source\": \"ctx._source.counter += params.count\",\n" +

" \"lang\": \"painless\",\n" +

" \"params\" : {\n" +

" \"count\" : 4\n" +

" }\n" +

" }\n" +

"}", ContentType.APPLICATION_JSON);

Response response= restClient.performRequest(method,endpoint, Collections.emptyMap(),entity);

System.out.println(EntityUtils.toString(response.getEntity()));

}/*** 4.部分文档更新增加字段(不存在name字段),存在则覆盖

*@throwsException*/@Testpublic void SomeUpdateDocument()throwsException{

String method= "POST";

String endpoint= "/update_index/test/1/_update";

HttpEntity entity= newNStringEntity("{\n" +

" \"doc\" : {\n" +

" \"user\" : \"kimchy\"\n" +

" }\n" +

"}", ContentType.APPLICATION_JSON);

Response response= restClient.performRequest(method,endpoint, Collections.emptyMap(),entity);

System.out.println(EntityUtils.toString(response.getEntity()));

}/*** 5.按查询API更新*/}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值