Elasticsearch之使用RestClient实现对数据的更新操作

版本:elasticsearch 7.13.4

1. 声明

当前内容主要用于本人学习和测试Elasticsearch的对数据的更新操作,主要参考官方文档

主要内容(JSON为fastjson的使用):

  1. 直接更新文档(更新文档中的所有数据)
  2. 更新文档中的部分数据
  3. 并发更新(基于if_seq_no=值&if_primary_term=值方式更新,非version)
  4. 可以在更新的后面使用?refresh来强制刷新内存数据

当前内容基于前面的博文

2. 直接更新文档(该方法和add一样)

	/**
	 * 
	 * @author hy
	 * @createTime 2021-07-31 11:10:39
	 * @description 修改数据操作
	 * @param restClient
	 * @throws IOException
	 *
	 */
	private static void updateData(RestClient restClient) throws IOException {
		Request request = new Request("PUT", "/book/java/1");
		Book book = new Book();
		book.setBookName("java ee");
		book.setId(2);
		book.setPrice(66.6);
		// 注意这里出现了问题,查询后的数据中的字段count消失了,所以这里应该使用部分更新操作
		String bookJson = JSON.toJSONString(book);
		request.setJsonEntity(bookJson);
		Response response = restClient.performRequest(request);
		int statusCode = response.getStatusLine().getStatusCode();
		System.out.println(response);
		// Response{requestLine=PUT /book/java/1 HTTP/1.1, host=http://127.0.0.1:9200,
		// response=HTTP/1.1 200 OK}
	}

更新前
在这里插入图片描述
更新后
在这里插入图片描述

说明这种更新方式会直接替换文档中的全部内容

2.更新文档的部分内容

只更新_id=1中的bookName属性内容

/**
	 * 
	 * @author hy
	 * @createTime 2021-07-31 11:13:25
	 * @description 部分数据的更新操作
	 * @param restClient
	 * @throws IOException
	 *
	 */
	private static void updateDataOnPartAttr(RestClient restClient) throws IOException {
		// 注意这里不允许使用PUT方法,只允许POST
		Request request = new Request("POST", "/book/java/1/_update");
		String json = "{\"doc\":{\"bookName\":\"java update\"}}";
		request.setJsonEntity(json);
		Response response = restClient.performRequest(request);
		System.out.println(response);
		// Response{requestLine=POST /book/java/1/_update HTTP/1.1,
		// host=http://localhost:9200, response=HTTP/1.1 200 OK}
	}

更新后结果
在这里插入图片描述

3. 并发更新

/**
	 * 
	 * @author hy
	 * @createTime 2021-07-31 13:21:10
	 * @description 在并发控制下的版本更新
	 * @param restClient
	 * @throws IOException
	 *
	 */
	private static void updateDataWhenConcurrency(RestClient restClient) throws IOException {
		// 注意这里不允许使用PUT方法,只允许POST
		// Request request = new Request("POST", "/book/java/1/_update?version=2");
		// 错误不能使用version方式控制乐观并发,直接报错了
		// {"error":{"root_cause":[{"type":"action_request_validation_exception","reason":"Validation
		// Failed: 1: internal versioning can not be used for optimistic concurrency
		// control. Please use `if_seq_no` and `if_primary_term`
		// instead;"}],"type":"action_request_validation_exception","reason":"Validation
		// Failed: 1: internal versioning can not be used for optimistic concurrency
		// control. Please use `if_seq_no` and `if_primary_term`
		// instead;"},"status":400}
		// Request request = new Request("POST", "/book/java/1/_update?if_seq_no=2");
		// 错误,存在if_seq_no属性,但是又需要primary term is [0]
		// {"error":{"root_cause":[{"type":"action_request_validation_exception","reason":"Validation
		// Failed: 1: ifSeqNo is set, but primary term is
		// [0];"}],"type":"action_request_validation_exception","reason":"Validation
		// Failed: 1: ifSeqNo is set, but primary term is [0];"},"status":400}
		Request request = new Request("POST", "/book/java/1/_update?if_seq_no=6&if_primary_term=2");
		String json = "{\"doc\":{\"bookName\":\"java update 3\"}}";
		request.setJsonEntity(json);
		Response response = restClient.performRequest(request);
		System.out.println(response);
		// 所以在当前7.13.4版本中是不可以使用version方式控制乐观并发的,必须使用if_seq_no=实际值和if_primary_termm=实际值方式来进行并发更新操作
		// Response{requestLine=POST /book/java/1/_update?if_seq_no=2&if_primary_term=1
		// HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}
	}

1.之前的使用version方式是无法执行乐观并发操作
2. 现在修改为if_seq_no和if_primary_term一起使用来控制,如果使用错误的话就无法更新

执行结果
在这里插入图片描述
在这里插入图片描述
测试成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值