更新文档
package com.wyh; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.databind.ObjectMapper; import com.wyh.config.ElasticSearchClientConfig; import com.wyh.entity.User; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.search.suggest.completion.RegexOptions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; import javax.annotation.Resource; import java.io.IOException; /** * 测试es7.6.1 高级客户端API操作 **/ @SpringBootTest class WyhApplicationTests { //注入bean @Resource private RestHighLevelClient client; //更新文档信息 @Test void testUpdateDocument() throws IOException { //更新请求 UpdateRequest updateRequest = new UpdateRequest("wei_index","1"); //超时时间 1s updateRequest.timeout("1s"); //创建user对象 User user = new User("魏二鹤", 18); //对象转json String jsonString = JSON.toJSONString(user); updateRequest.doc(jsonString,XContentType.JSON); UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT); System.out.println(updateResponse); System.out.println(updateResponse.status()); //UpdateResponse[index=wei_index,type=_doc,id=1,version=2,seqNo=1,primaryTerm=1,result=updated,shards=ShardInfo{total=2, successful=1, failures=[]}] //OK } }
网络异常,图片无法展示
|
查看数据库发现已经被更新成功
网络异常,图片无法展示
|
删除文档
package com.wyh; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.databind.ObjectMapper; import com.wyh.config.ElasticSearchClientConfig; import com.wyh.entity.User; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.search.suggest.completion.RegexOptions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; import javax.annotation.Resource; import java.io.IOException; /** * 测试es7.6.1 高级客户端API操作 **/ @SpringBootTest class WyhApplicationTests { //注入bean @Resource private RestHighLevelClient client; //删除文档信息 @Test void testDeleteDocument() throws IOException { //更新请求 DeleteRequest deleteRequest = new DeleteRequest("wei_index","1"); //超时时间 1s deleteRequest.timeout("1s"); DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(deleteResponse); //DeleteResponse[index=wei_index,type=_doc,id=1,version=3,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}] } }
查看索引库发现文档已经被删除
网络异常,图片无法展示
|
批量插入
package com.wyh; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.databind.ObjectMapper; import com.wyh.config.ElasticSearchClientConfig; import com.wyh.entity.User; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.search.suggest.completion.RegexOptions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; import javax.annotation.Resource; import java.io.IOException; import java.util.ArrayList; /** * 测试es7.6.1 高级客户端API操作 **/ @SpringBootTest class WyhApplicationTests { //注入bean @Resource private RestHighLevelClient client; //特殊的 ElasticSearch批量插入数据 @Test void testBatchInsert() throws IOException { BulkRequest bulkRequest = new BulkRequest(); //设置过期时间 bulkRequest.timeout("10s"); //泛型为User的list ArrayList<User> userArrayList = new ArrayList<>(); //添加用户信息 userArrayList.add(new User("魏一鹤1",14)); userArrayList.add(new User("魏一鹤2",14)); userArrayList.add(new User("魏一鹤3",14)); userArrayList.add(new User("魏一鹤4",14)); userArrayList.add(new User("魏一鹤5",14)); //批量添加数据 for (int i = 0; i < userArrayList.size(); i++) { //批量删除或者插入就在这里修改对应的请求即可 bulkRequest.add( //插入那个库 new IndexRequest("wei_index") //id .id(""+(i+1)) //json数据源 .source(JSON.toJSONString(userArrayList.get(i)),XContentType.JSON) ); } //执行批量插入请求并且获取相应 BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT); //是否执行失败 如果是false就没有失败 System.out.println(bulk.hasFailures());//false } }
查看库里发现已经插入成功
网络异常,图片无法展示
|
如果指名id会随机生成很长的id,防止数据重复
网络异常,图片无法展示
|
网络异常,图片无法展示
|