我想使用 ElasticSearch 和 Spring Data。我添加了这个依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
我想使用自动配置。我遵循这个文档:https ://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-connecting-to-elasticsearch-rest
If you have the org.elasticsearch.client:elasticsearch-rest-high-level-client
dependency on the classpath, Spring Boot will auto-configure a RestHighLevelClient
我创建了存储库:
@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, String> {
Page<Article> findByAuthorsName(String name, Pageable pageable);
}
我添加了这个,但它现在可以工作了!
启动我的应用程序后出现错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'articleRepository' defined in com.example.elasticsearch.repository.ArticleRepository defined in @EnableElasticsearchRepositories declared on Config:
Cannot resolve reference to bean 'elasticsearchTemplate' while setting bean property 'elasticsearchOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'elasticsearchTemplate' available
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:342)
但是为什么会这样呢?我想使用自动配置,我不想手动描述 bean,正如在对 stackoverflow 的其他响应中所说的那样!
我使用 Spring boot 2.3.1.RELEASE 和 Java 11
编辑:
我的配置:
@Configuration
@EnableElasticsearchRepositories(basePackages = " com.example.elasticsearch.repository")
public class Config {
}
我的应用程序:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}