Reputation: 1399
I want use ElasticSearch and Spring Data. I added this dependencies:
<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>
I want use Autoconfiguration. I follow this doc: 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
I created repository:
@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, String> {
Page<Article> findByAuthorsName(String name, Pageable pageable);
}
I added this, but it now working!
I get error after start my app:
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)
But why is this happening? I want to use autoconfiguration, I don't want to manually describe beans, as it is said in other responses to stackoverflow!
I use Spring boot 2.3.1.RELEASE and Java 11
EDIT:
My config:
@Configuration
@EnableElasticsearchRepositories(basePackages = " com.example.elasticsearch.repository")
public class Config {
}
My Application:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Upvotes: 0
Views: 4603
Reputation: 59221
I've tried to reproduce your setup locally and everything works fine.
As asked in the question's comments, I enabled the debug output to get the auto-configuration report for my app and I'm getting:
ElasticsearchDataAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.data.elasticsearch.core.ElasticsearchTemplate' (OnClassCondition)
ElasticsearchDataConfiguration.BaseConfiguration#elasticsearchConverter matched:
- @ConditionalOnMissingBean (types: org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; SearchStrategy: all) did not find any beans (OnBeanCondition)
ElasticsearchDataConfiguration.BaseConfiguration#mappingContext matched:
- @ConditionalOnMissingBean (types: org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; SearchStrategy: all) did not find any beans (OnBeanCondition)
ElasticsearchDataConfiguration.RestClientConfiguration matched:
- @ConditionalOnClass found required class 'org.elasticsearch.client.RestHighLevelClient' (OnClassCondition)
ElasticsearchDataConfiguration.RestClientConfiguration#elasticsearchTemplate matched:
- @ConditionalOnBean (types: org.elasticsearch.client.RestHighLevelClient; SearchStrategy: all) found bean 'elasticsearchRestHighLevelClient'; @ConditionalOnMissingBean (names: elasticsearchTemplate types: org.springframework.data.elasticsearch.core.ElasticsearchOperations; SearchStrategy: all) did not find any beans (OnBeanCondition)
The ElasticsearchRestTemplate
is created by Spring Boot, as expected. Please update your question with the requested information.
Upvotes: 1
Reputation: 149
Auto configuration will assume few things to configure your context, however you still need to wire the bean into your class to work with it.
Verify all your dependencies are imported as part of your build and then make sure to wire the elastic search template bean into your class.
If you still get the issue, you can post your class to get a better insight.
Hope this helps!
Upvotes: 0