ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
```java import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpHost; 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.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.junit.Test; import java.io.IOException; @Slf4j public class DocumentOperation { /** * 新增文档 */ @Test public void insertDocument() throws IOException { //获取elasticsearch客户端 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //创建请求对象 IndexRequest request = new IndexRequest(); //设置索引与此次新增的文档id request.index("user").id("1001"); //插入的数据 User user = User.builder().name("zhangsan").age(30).sex("男").build(); //向elasticsearch插入数据,必须将数据转换位JSON格式 ObjectMapper mapper = new ObjectMapper(); String userJson = mapper.writeValueAsString(user); request.source(userJson, XContentType.JSON); //发送请求并获取响应 IndexResponse response = client.index(request, RequestOptions.DEFAULT); log.info("{}", response.toString()); //IndexResponse[index=user,type=_doc,id=1001,version=1, // result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}] //关闭连接 client.close(); } /** * 查询文档 */ @Test public void searchDocument() throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //设置查询的索引与文档id GetRequest request = new GetRequest().index("user").id("1001"); //发送请求并获取响应 GetResponse response = client.get(request, RequestOptions.DEFAULT); log.info(response.toString()); //{"_index":"user","_type":"_doc","_id":"1001","_version":1,"_seq_no":0," // _primary_term":1,"found":true,"_source":{"name":"zhangsan","sex":"男","age":30}} } /** * 修改文档 * 如果文档不存在则抛出异常 */ @Test public void updateDocument() throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); UpdateRequest request = new UpdateRequest(); //要修改的索引与文档id request.index("user").id("1001"); //要修改的字段 request.doc(XContentType.JSON, "age", 26, "sex", "女"); //发送请求并获取响应 UpdateResponse response = client.update(request, RequestOptions.DEFAULT); log.info(response.toString()); //UpdateResponse[index=user,type=_doc,id=1001,version=2,seqNo=1,primaryTerm=1, // result=updated,shards=ShardInfo{total=2, successful=1, failures=[]}] client.close(); } /** * 删除文档 */ @Test public void deleteDocument() throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //设置索引与文档id DeleteRequest request = new DeleteRequest().index("user").id("1001"); //客户端发送请求,获取响应对象 DeleteResponse response = client.delete(request, RequestOptions.DEFAULT); //DeleteResponse[index=user,type=_doc,id=1001,version=8,result=deleted // ,shards=ShardInfo{total=2, successful=1, failures=[]}] log.info(response.toString()); client.close(); } /** * 批量新增 */ @Test public void batchInsertDocument() throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //批量请求对象 BulkRequest request = new BulkRequest(); //批量添加数据 request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan", "age", 30, "sex", "男")); request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi", "age", 30, "sex", "女")); request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu", "age", 40, "sex", "男")); request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "wangwu1", "age", 40, "sex", "女")); request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "wangwu2", "age", 50, "sex", "男")); request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON, "name", "wangwu3", "age", 50, "sex", "男")); request.add(new IndexRequest().index("user").id("1007").source(XContentType.JSON, "name", "wangwu44", "age", 60, "sex", "男")); request.add(new IndexRequest().index("user").id("1008").source(XContentType.JSON, "name", "wangwu555", "age", 60, "sex", "男")); request.add(new IndexRequest().index("user").id("1009").source(XContentType.JSON, "name", "wangwu66666", "age", 60, "sex", "男")); //发送请求并获取响应 BulkResponse response = client.bulk(request, RequestOptions.DEFAULT); //_took(花费的时间),59ms log.info("_took(花费的时间),{}", response.getTook()); client.close(); } /** * 批量删除 */ @Test public void batchDeletDocument() throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //批量删除数据 BulkRequest request = new BulkRequest(); //批量删除 request.add(new DeleteRequest().index("user").id("1001")); request.add(new DeleteRequest().index("user").id("1002")); request.add(new DeleteRequest().index("user").id("1003")); //发送请求并获取响应 BulkResponse response = client.bulk(request, RequestOptions.DEFAULT); //_took(花费的时间),119ms log.info("_took(花费的时间),{}", response.getTook()); client.close(); } } ```