大數據技術之Elasticsearch-Java API操作(一)API基本操作
更新文檔數據(update)
1)源代碼
@Test public void updateData() throws Throwable { // 1 創建更新數據的請求對象 UpdateRequest updateRequest = new UpdateRequest(); updateRequest.index("blog"); updateRequest.type("article"); updateRequest.id("3"); updateRequest.doc(XContentFactory.jsonBuilder().startObject() // 對沒有的字段添加, 對已有的字段替換 .field("title", "基於Lucene的搜索服務器") .field("content","它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口。大數據前景無限") .field("createDate", "2017-8-22").endObject()); // 2 獲取更新后的值 UpdateResponse indexResponse = client.update(updateRequest).get(); // 3 打印返回的結果 System.out.println("index:" + indexResponse.getIndex()); System.out.println("type:" + indexResponse.getType()); System.out.println("id:" + indexResponse.getId()); System.out.println("version:" + indexResponse.getVersion()); System.out.println("create:" + indexResponse.getResult()); // 4 關閉連接 client.close(); } |
2)結果查看
****自己操作****
Java代碼:
// 七、更新文檔-update
@Test public void updateData() throws Exception { // 創建更新數據的請求對象
UpdateRequest updateRequest = new UpdateRequest(); updateRequest.index("blog"); updateRequest.type("article"); updateRequest.id("Rw3d8XMBfpkCch_aW3Da"); updateRequest.doc(XContentFactory.jsonBuilder().startObject() // 對沒有的字段添加, 對已有的字段替換
.field("title", "基於Lucene的搜索服務器").field("content", "它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口。大數據前景無限") .field("createDate", "2017-8-22").endObject()); UpdateResponse indexResponse = client.update(updateRequest).get(); // 打印返回結果
System.out.println("index:" + indexResponse.getIndex()); System.out.println("type:" + indexResponse.getType()); System.out.println("id:" + indexResponse.getId()); System.out.println("version:" + indexResponse.getVersion()); System.out.println("result:" + indexResponse.getResult()); // 關閉連接
client.close(); }
結果: