以下是关于 Spring Boot 集成 spring-boot-starter-data-elasticsearch 的详细代码示例和配置说明,帮助您快速实现与 Elasticsearch 的集成。
1. 添加依赖
在 pom.xml
文件中添加 spring-boot-starter-data-elasticsearch
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
确保 Spring Boot 版本与 Elasticsearch 版本兼容。例如,Spring Boot 2.x 通常与 Elasticsearch 6.x 或 7.x 兼容。
2. 配置 Elasticsearch 连接
在 application.yml
或 application.properties
文件中配置 Elasticsearch 的连接信息:
spring:
data:
elasticsearch:
cluster-name: elasticsearch # 集群名称
cluster-nodes: 192.168.1.33:9300 # 集群节点地址和端口
repositories:
enabled: true
properties:
transport:
tcp:
connect_timeout: 120s # 连接超时时间
如果是单机模式,可以直接配置 spring.elasticsearch.rest.uris
:
spring:
elasticsearch:
rest:
uris: http://localhost:9200
3. 定义实体类
使用 @Document
注解定义 Elasticsearch 索引和映射:
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data
@Document(indexName = "blog_1") // 索引名称
public class Blog {
@Id
private String id;
@Field(type = FieldType.Text, analyzer = "ik_max_word") // 使用 IK 分词器
private String title;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String content;
@Field(type = FieldType.Date)
private Date publishDate;
}
4. 创建 Repository 接口
继承 ElasticsearchRepository
接口,定义 CRUD 操作和自定义查询方法:
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface BlogRepository extends ElasticsearchRepository<Blog, String> {
List<Blog> findByTitle(String title); // 根据标题查询
List<Blog> findByContentContaining(String keyword); // 根据内容模糊查询
}
5. 业务逻辑中使用 Repository
在 Service 或 Controller 中使用 BlogRepository
执行 Elasticsearch 操作:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BlogService {
@Autowired
private BlogRepository blogRepository;
public void saveBlog(Blog blog) {
blogRepository.save(blog);
}
public List<Blog> searchByTitle(String title) {
return blogRepository.findByTitle(title);
}
public List<Blog> searchByContent(String keyword) {
return blogRepository.findByContentContaining(keyword);
}
}
6. 版本兼容性问题及解决方案
- 问题:Spring Boot 和 Elasticsearch 版本不兼容可能导致运行时异常,例如
type is missing
错误。 - 解决方案:确保 Spring Boot 和 Elasticsearch 版本匹配。例如:
- Spring Boot 2.x 推荐使用 Elasticsearch 6.x 或 7.x。
- 如果使用 Elasticsearch 7.x,注意
@Document
注解中的type
属性已被废弃。
7. 中文分词器配置
如果需要支持中文分词,可以集成 IK 分词器:
- 下载与 Elasticsearch 版本匹配的 IK 分词器插件。
- 在实体类的字段中指定分词器:
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String content;
8. 总结
通过以上步骤,您可以快速实现 Spring Boot 与 Elasticsearch 的集成,并完成数据的存储、查询等操作。如果需要更详细的配置或解决特定问题,可以参考 Spring Data Elasticsearch 官方文档 或上述来源。
在Spring Boot项目中,想要集成Elasticsearch,你需要在项目的 pom.xml
文件中添加 spring-boot-starter-data-elasticsearch
依赖。以下是添加依赖的一个示例:
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Data Elasticsearch -->
<dependency>