spring boot整合 es 7.9.0版本, 给出详细代码, 要求扩展性强
时间: 2024-02-20 15:01:25 浏览: 31
好的,下面是详细的代码,其中包含了Spring Boot和Elasticsearch 7.9.0版本的整合,实现了基本的CURD操作,并且具有扩展性:
1. 添加依赖
首先,在pom.xml中添加以下依赖:
```xml
<dependencies>
<!-- Elasticsearch 7.9.0 -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.9.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.9.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.9.0</version>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
```
2. 配置文件
在application.properties中配置Elasticsearch连接信息:
```properties
elasticsearch.host=127.0.0.1
elasticsearch.port=9200
elasticsearch.scheme=http
```
3. 编写配置类
创建一个Elasticsearch配置类,用于创建Elasticsearch客户端:
```java
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
@Value("${elasticsearch.host}")
private String host;
@Value("${elasticsearch.port}")
private int port;
@Value("${elasticsearch.scheme}")
private String scheme;
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(host + ":" + port)
.usingSsl("https".equals(scheme))
.build();
return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchRestTemplate elasticsearchTemplate() {
return new ElasticsearchRestTemplate(elasticsearchClient());
}
}
```
4. 编写实体类
创建一个实体类,用于映射Elasticsearch中的文档:
```java
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Data
@Document(indexName = "article")
public class Article {
@Id
private String id;
private String title;
private String content;
}
```
5. 编写DAO接口
创建一个DAO接口,继承自ElasticsearchRepository,用于操作Elasticsearch中的文档:
```java
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ArticleDao extends ElasticsearchRepository<Article, String> {
}
```
6. 编写服务类
创建一个服务类,用于对Elasticsearch中的文档进行CURD操作:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class ArticleService {
@Autowired
private ArticleDao articleDao;
public void save(Article article) {
articleDao.save(article);
}
public Optional<Article> findById(String id) {
return articleDao.findById(id);
}
public void deleteById(String id) {
articleDao.deleteById(id);
}
}
```
7. 编写控制器
创建一个控制器,用于对Elasticsearch中的文档进行RESTful API操作:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController
@RequestMapping("/article")
public class ArticleController {
@Autowired
private ArticleService articleService;
@PostMapping
public void save(@RequestBody Article article) {
articleService.save(article);
}
@GetMapping("/{id}")
public Optional<Article> findById(@PathVariable String id) {
return articleService.findById(id);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable String id) {
articleService.deleteById(id);
}
}
```
这样,我们就实现了基本的Elasticsearch CURD操作,并且具有扩展性。我们可以根据需要,对DAO接口和服务类进行扩展,实现更加复杂的操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)