SpringBoot整合ElasticSearch

1、导入依赖:

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  4. <version>7.9.0</version>
  5. </dependency>

2、配置

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <!--自定义版本依赖,保证和本地一致-->
  4. <elasticsearch.version>7.9.0</elasticsearch.version>
  5. </properties>

3、ES配置类

  1. package com.zym.test.config;
  2. import org.apache.http.HttpHost;
  3. import org.elasticsearch.client.RestClient;
  4. import org.elasticsearch.client.RestHighLevelClient;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. @Configuration
  8. public class ElasticSearchConfig {
  9. @Bean
  10. public RestHighLevelClient restHighLevelClient(){
  11. RestHighLevelClient client = new RestHighLevelClient(
  12. RestClient.builder(
  13. new HttpHost("localhost",9200,"http")
  14. )
  15. );
  16. return client;
  17. }
  18. }

4、测试

  1. package com.zym.test;
  2. import org.elasticsearch.client.RequestOptions;
  3. import org.elasticsearch.client.RestHighLevelClient;
  4. import org.elasticsearch.client.indices.CreateIndexRequest;
  5. import org.elasticsearch.client.indices.CreateIndexResponse;
  6. import org.junit.jupiter.api.Test;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import java.io.IOException;
  10. @SpringBootTest
  11. class SpringbootEsApplicationTests {
  12. @Autowired
  13. private RestHighLevelClient restHighLevelClient;
  14. //索引的操作
  15. //创建索引
  16. @Test
  17. public void createIndex() throws IOException {
  18. CreateIndexRequest request = new CreateIndexRequest("test_index");
  19. CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
  20. System.out.println(response);
  21. }
  22. }

1.png

  1. //获取索引
  2. @Test
  3. public void getIndex() throws IOException {
  4. GetIndexRequest request = new GetIndexRequest("test_index");
  5. boolean exists = restHighLevelClient.indices().exists(request,RequestOptions.DEFAULT);
  6. System.out.println(exists);
  7. }

2.png

  1. //删除索引
  2. @Test
  3. public void deleteIndex() throws IOException {
  4. DeleteIndexRequest request = new DeleteIndexRequest("test_index");
  5. AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
  6. System.out.println(response.isAcknowledged());
  7. }

3.png

  1. //添加文档
  2. @Test
  3. public void addDocument() throws IOException {
  4. User user = new User("zym",3);
  5. IndexRequest index = new IndexRequest("test_index");
  6. index.id("1");
  7. index.timeout(TimeValue.MINUS_ONE);
  8. index.source(JSON.toJSONString(user), XContentType.JSON);
  9. IndexResponse response = restHighLevelClient.index(index, RequestOptions.DEFAULT);
  10. System.out.println(response.status());
  11. }

4.png
5.png

  1. //获取文档
  2. @Test
  3. public void isExistsDocument() throws IOException {
  4. GetRequest request = new GetRequest("test_index","1");
  5. //不获取返回的_source上下文
  6. request.fetchSourceContext(new FetchSourceContext(false));
  7. request.storedFields("_none_");
  8. boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
  9. System.out.println(exists);
  10. }

6.png

  1. //获取信息
  2. @Test
  3. public void getDocument() throws IOException {
  4. GetRequest request = new GetRequest("test_index","1");
  5. GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
  6. System.out.println(response.getSourceAsString());
  7. System.out.println(response);
  8. }

7.png

  1. //更新
  2. @Test
  3. public void updateDocument() throws IOException {
  4. UpdateRequest request = new UpdateRequest("test_index","1");
  5. request.timeout("1s");
  6. User user = new User("张三", 24);
  7. request.doc(JSON.toJSONString(user),XContentType.JSON);
  8. UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
  9. System.out.println(response);
  10. }

8.png
9.png

  1. //删除
  2. @Test
  3. public void deleteDocument() throws IOException {
  4. DeleteRequest request = new DeleteRequest("test_index","1");
  5. request.timeout("1s");
  6. DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
  7. System.out.println(response.status());
  8. }

10.png

  1. //批量插入
  2. @Test
  3. public void batchAddDocument() throws IOException {
  4. BulkRequest bulkRequest = new BulkRequest();
  5. bulkRequest.timeout("10s");
  6. List<User> userList = new ArrayList<User>();
  7. userList.add(new User("zym",1));
  8. userList.add(new User("zym",2));
  9. userList.add(new User("zym",3));
  10. userList.add(new User("zym",4));
  11. userList.add(new User("zym",5));
  12. for (int i = 0; i < userList.size(); i++) {
  13. bulkRequest.add(new IndexRequest("test_index").id(""+(i+1)).source(JSON.toJSONString(userList.get(i)),XContentType.JSON));
  14. }
  15. BulkResponse responses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
  16. System.out.println(responses.status());
  17. }

11.png
12.png

  1. //查询
  2. @Test
  3. public void search() throws IOException {
  4. SearchRequest searchRequest = new SearchRequest("test_index");
  5. //构建搜索条件
  6. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  7. TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zym");
  8. searchSourceBuilder.query(termQueryBuilder);
  9. searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
  10. searchRequest.source(searchSourceBuilder);
  11. SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  12. System.out.println(JSON.toJSONString(response.getHits()));
  13. }

13.png