ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
springboot版本:2.3.4 elasticsearch版本:7.9.2 ***** ### 1.引入依赖 ``` <!-- springboot默认使用springData elasticsearch模块进行操作--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` ***** ### 2.创建配置 ``` package com.youge.elasticsearch.config; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-10-20 */ @Configuration public class ElasticSearchConfig { @Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.1.5",9200,"http"))); return restHighLevelClient; } } ``` ***** ### 3.测试 ``` package com.youge.elasticsearch; import com.alibaba.fastjson.JSON; import com.youge.elasticsearch.dao.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.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.xcontent.XContentType; 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.ArrayList; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-10-20 4:19 */ @RunWith(SpringRunner.class) @SpringBootTest public class ElasticsearchTest { @Autowired private RestHighLevelClient restHighLevelClient; /** * 创建索引 * * @throws IOException */ @Test public void createIndexTest() throws IOException { CreateIndexRequest createIndexRequest = new CreateIndexRequest("hcf"); CreateIndexResponse response = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); System.out.println(response.toString()); } /** * 检测索引是否存在 * * @throws IOException */ @Test public void existIndexTest() throws IOException { GetIndexRequest request = new GetIndexRequest("hcf"); boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } /** * 删除索引 * * @throws IOException */ @Test public void deleteIndexTest() throws IOException { DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("hcf"); AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); } /** * 测试添加文档 * * @throws IOException */ @Test public void createDocumentTest() throws IOException { User user = new User("hcf", 30); IndexRequest request = new IndexRequest("hcf"); request.id("1"); request.timeout("1s"); //将数据放入请求 request.source(JSON.toJSONString(user), XContentType.JSON); IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT); System.out.println(index.toString()); System.out.println(index.status()); } /** * 获取文档 * * @throws IOException */ @Test public void getDocumentTest() throws IOException { GetRequest getRequest = new GetRequest("hcf", "1"); GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); System.out.println(response.getSourceAsString()); System.out.println(response.getSourceAsMap().get("name")); System.out.println(response.getSourceAsMap().get("age")); System.out.println(response); } /** * 更新文档信息 * * @throws IOException */ @Test public void updateDocumentTest() throws IOException { UpdateRequest request = new UpdateRequest("hcf", "1"); request.timeout("1s"); User user = new User("hcf java", 28); request.doc(JSON.toJSONString(user), XContentType.JSON); UpdateResponse update = restHighLevelClient.update(request, RequestOptions.DEFAULT); System.out.println(update); System.out.println(update.status()); } /** * 删除文档 * * @throws IOException */ @Test public void deleteDocumentTest() throws IOException { DeleteRequest request = new DeleteRequest("hcf", "1"); request.timeout("10s"); User user = new User("hcf java", 28); DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT); System.out.println(delete.status()); } /** * 批量插入数据 * @throws IOException */ @Test public void BulkRequestTest() throws IOException { BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("10s"); ArrayList<User> users = new ArrayList<>(); users.add(new User("zhangsan", 1)); users.add(new User("lishi", 12)); users.add(new User("wangwu", 13)); users.add(new User("zhaoliu", 14)); users.add(new User("tianqi", 15)); for (int i = 0; i < users.size(); i++) { bulkRequest.add( new IndexRequest("hcf").id("" + i + 1).source(JSON.toJSONString(users.get(i)), XContentType.JSON) ); BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulk); } } } ```