Springboot如何整合elasticsearch
时间: 2023-03-10 08:51:25 浏览: 118
Springboot可以通过官方提供的Elasticsearch Starter来整合elasticsearch,它可以自动配置Elasticsearch客户端和提供Elasticsearch操作的工具。
相关问题
springboot整合elasticSearch
Spring Boot可以很方便地与Elasticsearch进行集成。下面是一个简单的示例:
1. 添加Elasticsearch客户端依赖
在pom.xml中添加以下依赖:
```xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.0</version>
</dependency>
```
2. 配置Elasticsearch客户端
在application.properties中添加以下配置:
```properties
spring.elasticsearch.rest.uris=http://localhost:9200
```
3. 创建Elasticsearch客户端Bean
```java
@Configuration
public class ElasticsearchConfig {
@Value("${spring.elasticsearch.rest.uris}")
private String elasticsearchUrls;
@Bean
public RestHighLevelClient restHighLevelClient() {
String[] urls = elasticsearchUrls.split(",");
HttpHost[] httpHosts = new HttpHost[urls.length];
for (int i = 0; i < urls.length; i++) {
String url = urls[i];
httpHosts[i] = new HttpHost(url.split(":")[0], Integer.parseInt(url.split(":")[1]), "http");
}
RestClientBuilder builder = RestClient.builder(httpHosts);
return new RestHighLevelClient(builder);
}
}
```
4. 创建Elasticsearch操作类
```java
@Component
public class ElasticsearchTemplate {
@Autowired
private RestHighLevelClient restHighLevelClient;
public void index(String index, String id, String json) throws IOException {
IndexRequest request = new IndexRequest(index);
request.id(id);
request.source(json, XContentType.JSON);
restHighLevelClient.index(request, RequestOptions.DEFAULT);
}
public SearchHits search(String index, String keyword) throws IOException {
SearchRequest request = new SearchRequest(index);
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.matchQuery("name", keyword));
request.source(builder);
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
return response.getHits();
}
}
```
5. 使用Elasticsearch操作类
```java
@RestController
public class ElasticsearchController {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@PostMapping("/index")
public void index(String index, String id, String json) throws IOException {
elasticsearchTemplate.index(index, id, json);
}
@GetMapping("/search")
public SearchHits search(String index, String keyword) throws IOException {
return elasticsearchTemplate.search(index, keyword);
}
}
```
这就是一个简单的Spring Boot整合Elasticsearch的示例。你可以根据自己的需求进行修改和扩展。
SpringBoot整合Elasticsearch
Spring Boot整合Elasticsearch可以使用Spring Data Elasticsearch,在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
```
在application.properties配置文件中添加以下配置:
```
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-name=my-application
spring.data.elasticsearch.cluster-nodes=localhost:9300
```
其中,cluster-name为Elasticsearch集群名称,cluster-nodes为Elasticsearch集群节点地址。
接下来,定义一个实体类,并使用注解标注该实体类对应的索引和类型:
```java
@Document(indexName = "article", type = "news")
public class Article {
@Id
private Long id;
@Field(analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String title;
@Field(analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String content;
// getter/setter
}
```
在定义完实体类后,可以使用ElasticsearchRepository接口定义数据访问层的方法:
```java
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
List<Article> findByTitle(String title);
}
```
在服务层中调用ArticleRepository的方法即可进行数据的增删改查操作:
```java
@Service
public class ArticleService {
@Autowired
private ArticleRepository articleRepository;
public void save(Article article) {
articleRepository.save(article);
}
public void delete(Long id) {
articleRepository.deleteById(id);
}
public Article getById(Long id) {
Optional<Article> optional = articleRepository.findById(id);
return optional.isPresent() ? optional.get() : null;
}
public List<Article> getByTitle(String title) {
return articleRepository.findByTitle(title);
}
}
```
以上就是Spring Boot整合Elasticsearch的基本流程。需要注意的是,在使用Spring Data Elasticsearch时,需要根据实际情况配置相应的注解和属性,以保证数据的正确存储和检索。
阅读全文