1 创建SpringBoot项目
网络异常,图片无法展示
|
2 引入ElasticSearch依赖
注意 要保证ES版本和本地一致(7.6.1)
网络异常,图片无法展示
|
网络异常,图片无法展示
|
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com</groupId> <artifactId>wyh</artifactId> <version>0.0.1-SNAPSHOT</version> <name>wyh</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <!--自定义版本ES依赖 保证和本地版本一致--> <elasticsearch.version>7.6.1</elasticsearch.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--ES依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!--spring web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency> <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.70</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3 配置ES高级客户端
package com.wyh.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; /** * @program: SpringBoot_ElasticSearch * @description: ElasticSearch相关配置 * @author: 魏一鹤 * @createDate: 2022-04-18 22:04 **/ //表面这是一个配置文件 @Configuration public class ElasticSearchClientConfig { //配置bean 注册 rest高级客户端 @Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( //ip 端口 协议 //一定要保证es服务是开启的 new HttpHost("127.0.0.1",9200,"http") ) ); return client; } }
4 关于索引的API操作
创建索引
package com.wyh; import com.wyh.config.ElasticSearchClientConfig; 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.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 javax.annotation.Resource; import java.io.IOException; /** * 测试es7.6.1 高级客户端API操作 **/ @SpringBootTest class WyhApplicationTests { //注入bean @Resource private RestHighLevelClient client; //创建索引库 request @Test void testCreateIndex() throws IOException { //1 创建索引请求 索引库名称叫wei_index CreateIndexRequest request = new CreateIndexRequest("wei_index"); //2 客户端执行创建请求 需要抛出IO异常IOException CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); //3 获得响应 打印响应 //org.elasticsearch.client.indices.CreateIndexResponse@423fb58d System.out.println(createIndexResponse); } }
查看客户端,发现创建成功
网络异常,图片无法展示
|
获取索引库
package com.wyh; import com.wyh.config.ElasticSearchClientConfig; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; 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.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 javax.annotation.Resource; import java.io.IOException; /** * 测试es7.6.1 高级客户端API操作 **/ @SpringBootTest class WyhApplicationTests { //注入bean @Resource private RestHighLevelClient client; //获取索引库,只能判断这个库存不存在 @Test void textExistIndex() throws IOException { //1 获取wei_index这个索引库请求 GetIndexRequest getIndexRequest = new GetIndexRequest("wei_index"); //2 执行获取索引库的操作 boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT); //3 查看结果 有索引库返回true,没有索引库返回false //true System.out.println(exists); } }