package es_01; import com.kwan.shuyu.SearchApplication; import lombok.extern.slf4j.Slf4j; import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * 更新文档 * * @author : qinyingjie * @version : 2.2.0 * @date : 2023/5/10 14:05 */ @Slf4j @SpringBootTest(classes = SearchApplication.class) @RunWith(SpringRunner.class) public class TestDocument_08_update { @Autowired RestHighLevelClient client; @Test public void testUpdate() throws IOException { //创建请求 UpdateRequest request = new UpdateRequest("test_post", "3"); Map jsonMap = new HashMap<>(); jsonMap.put("user", "tomas Lee"); request.doc(jsonMap); request.timeout("1s"); request.retryOnConflict(3);//重试次数 //同步执行 UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT); //获取结果 updateResponse.getId(); updateResponse.getIndex(); //判断结果 if (updateResponse.getResult() == DocWriteResponse.Result.CREATED) { DocWriteResponse.Result result = updateResponse.getResult(); log.info("CREATED:" + result); } else if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) { DocWriteResponse.Result result = updateResponse.getResult(); log.info("UPDATED:" + result); } else if (updateResponse.getResult() == DocWriteResponse.Result.DELETED) { DocWriteResponse.Result result = updateResponse.getResult(); log.info("DELETED:" + result); } else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) { //没有操作 DocWriteResponse.Result result = updateResponse.getResult(); log.info("NOOP:" + result); } } }