img

导入Maven依赖


<!--ElasticSearch-->
<!--SpringBoot 集成的这个过期了 -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-elasticsearch</artifactId>-->
<!-- </dependency>-->
<!-- 这个一定要引入,这是使用transport的jar包,搜索用到 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.9.2</version>
</dependency>

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.9.2</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.9.2</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>

配置ElasticSearchConfig类

全局使用ElasticSearch

package com.bookmanager.www.common.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
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.client.indices.GetIndexResponse;

/**
* Created with IntelliJ IDEA.
*
* @author: 风离
* @Date: 2021/10/06/2:18
* @Description:
*/
@Configuration
public class ElasticSearchConfig {

@Bean
public RestHighLevelClient restHighLevelClient()
{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
return client;
}

}

简单测试

 /**
* 测试ElasticSearch
*/
@Test
void TestElasticSearch() throws IOException {
//创建ES连接客户端 有配置类这里就不需要了
// RestHighLevelClient client = new RestHighLevelClient(
// RestClient.builder(new HttpHost("localhost",9200,"http"))
// );

// 创建索引
CreateIndexRequest index = new CreateIndexRequest("fl-user");
CreateIndexResponse createIndexResponse = client.indices().create(index, RequestOptions.DEFAULT);
//查询索引
GetIndexRequest getIndex = new GetIndexRequest("fl-user");
GetIndexResponse getIndexResponse = client.indices().get(getIndex, RequestOptions.DEFAULT);
//是否创建成功
System.out.println(createIndexResponse.isAcknowledged());
//查询索引操作
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());

//删除索引
DeleteIndexRequest deleteIndex=new DeleteIndexRequest("fl-user");
AcknowledgedResponse deleteIndexResponse = client.indices().delete(deleteIndex,RequestOptions.DEFAULT);
//是否删除成功
System.out.println(deleteIndexResponse.isAcknowledged());
//关闭连接
client.close();
}

image-20211006030359222