要将Elasticsearch集成到Spring Boot中,您可以遵循以下步骤:

  1. 添加maven依赖项

在pom.xml文件中添加以下依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
  1. 配置Elasticsearch

在application.properties中添加以下配置:

spring.data.elasticsearch.cluster-name=my-application
spring.data.elasticsearch.cluster-nodes=localhost:9300

这里假设您正在使用本地安装的Elasticsearch集群。

  1. 创建Elasticsearch Repository

创建一个接口扩展ElasticsearchRepository:

public interface BookRepository extends ElasticsearchRepository<Book, String> {
}

这里假设您的实体类是Book,它有一个唯一的标识符。

  1. 注入Repository

在您的服务类中注入Repository:

@Service
public class BookService {

    @Autowired
    private BookRepository bookRepository;

    // ...
}

现在您可以使用bookRepository来进行Elasticsearch的操作了,例如:

bookRepository.save(book);
bookRepository.findById(id);
bookRepository.delete(book);

这就是如何将Elasticsearch快速集成到Spring Boot中的步骤。

Spring Boot快速集成Elasticsearch搜索

原文地址: http://www.cveoy.top/t/topic/nGYb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录