下面是关于相关Elasticsearch的API功能是一些增删查改功能,到时可根据自己需求进行修改
- 在idea中创建springboot项目,并选中Elasticsearch依赖
- 自定义elasticsearch版本,保证跟本地一致(由于我本机安装的是7.X版本的),同时再添加上相关json依赖。
<properties>
<java.version>1.8</java.version>
<!--自定义es版本依赖,保证和本地一致-->
<elasticsearch.version>7.6.1</elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.26</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
- 在application.properties添加上相关的配置
elasticsearch.hostname:127.0.0.1
elasticsearch.port:9200
elasticsearch.scheme:http
- 同时创建一个config包,并在该包下创建elasticSearchConfig.java的配置类
@Configuration
public class elasticSearchConfig {
@Value("${elasticsearch.hostname}")
private String hostname;
@Value("${elasticsearch.port}")
private int port;
@Value("${elasticsearch.scheme}")
private String scheme;
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost(hostname,port,scheme)));
return client;
}
}
- 创建service包,并在该包下创建EsSearchService接口,用于实现Elasticsearch的相关功能,同时在service包下再创建一个Impl包,并在该包下创建EsSearchServiceImpl.java.(下面的索引可以理解为建立一个表,然后文档可以理解为给这个表中添加每一行数据)
EsSearchService.java
public interface EsSearchService {
/**
* 创建索引
* @param index 索引的名称
* @return
*/