Spring Boot整合Elasticsearch的基本步骤如下:

  1. 添加依赖:在pom.xml中添加Elasticsearch的客户端依赖。
<dependencies>
    <!-- Elasticsearch REST client -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.10.2</version>
    </dependency>
    <!-- Elasticsearch Rest Hig Level Client 的依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
</dependencies>
  1. 配置Elasticsearch客户端:在application.propertiesapplication.yml中配置Elasticsearch的连接信息。
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.elasticsearch.rest.uris=http://localhost:9200
  1. 创建Repository接口:继承ElasticsearchRepository,提供基本的CRUD操作。
public interface MyEntityRepository extends ElasticsearchRepository<MyEntity, String> {
    // 自定义查询方法
}
  1. 使用Repository进行操作:在Service层注入Repository,并使用其提供的方法。
@Service
public class MyEntityService {
 
    @Autowired
    private MyEntityRepository repository;
 
    public MyEntity save(MyEntity entity) {
        return repository.save(entity);
    }
 
    public List<MyEntity> findAll() {
        return repository.findAll();
    }
 
    // 其他操作...
}