英文:
Bean method 'elasticsearchTemplate' not able to find in configuration
问题
获取以下错误:
应用启动失败
描述:
com.rahul.es.api.service.QueryDSLService 中的字段模板需要一个类型为 'org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate' 的 bean,但找不到该bean。
注入点具有以下注释:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
找到以下候选项,但无法注入:
- 在 'ElasticsearchDataConfiguration.RestClientConfiguration' 中的 bean 方法 'elasticsearchTemplate' 未加载,因为 @ConditionalOnMissingBean(名称:elasticsearchTemplate 类型:org.springframework.data.elasticsearch.core.ElasticsearchOperations;搜索策略:全部)找到类型为 'org.springframework.data.elasticsearch.core.ElasticsearchOperations' 的 bean elasticsearchTemplate 和找到名为 elasticsearchTemplate 的 bean
操作:
请考虑重新查看上述条目或在配置中定义类型为 'org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate' 的 bean。
EsConfig.java
@Configuration(proxyBeanMethods=false)
@EnableElasticsearchRepositories(basePackages = "com.rahul.es.api.repository")
@ComponentScan(basePackages = { "com.rahul.es.api.service" })
public class EsConfig {
@Bean
public RestHighLevelClient client() {
ClientConfiguration clientConfiguration
= ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchRestTemplate(client());
}
}
QueryDSLService
@Service
public class QueryDSLService {
@Autowired
private ElasticsearchRestTemplate template;
public List<Customer> searchMultipleField(String firstname, int age) {
QueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("firstname", firstname))
.must(QueryBuilders.matchQuery("age", age));
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withQuery(query).build();
List<Customer> customers = template.queryForList(nativeSearchQuery, Customer.class);
return customers;
}
public List<Customer> getCustomerSearchData(String input) {
String search = ".*" + input + ".*";
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withFilter(QueryBuilders.regexpQuery("firstname", search)).build();
List<Customer> customers = template.queryForList(searchQuery, Customer.class);
return customers;
}
public List<Customer> multiMatchQuery(String text) {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.multiMatchQuery(text)
.field("firstname").field("lastname").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build();
List<Customer> customers = template.queryForList(searchQuery, Customer.class);
return customers;
}
}
RestController
@SpringBootApplication
@RestController
public class SpringbootElasticsearchQuerydslApplication {
@Autowired
private QueryDSLService service;
@GetMapping("/serachMultiField/{firstname}/{age}")
public List<Customer> serachByMultiField(@PathVariable String firstname, @PathVariable int age) {
return service.searchMultipleField(firstname, age);
}
@GetMapping("/customSearch/{firstName}")
public List<Customer> getCustomerByField(@PathVariable String firstName) {
return service.getCustomerSearchData(firstName);
}
@GetMapping("/search/{text}")
public List<Customer> doMultimatchQuery(@PathVariable String text) {
return service.multiMatchQuery(text);
}
public static void main(String[] args) {
SpringApplication.run(SpringbootElasticsearchQuerydslApplication.class, args);
}
}
英文:
Getting Below Error:
APPLICATION FAILED TO START
Description:
Field template in com.rahul.es.api.service.QueryDSLService required a bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
The following candidates were found but could not be injected:
- Bean method 'elasticsearchTemplate' in 'ElasticsearchDataConfiguration.RestClientConfiguration' not loaded because @ConditionalOnMissingBean (names: elasticsearchTemplate types: org.springframework.data.elasticsearch.core.ElasticsearchOperations; SearchStrategy: all) found beans of type 'org.springframework.data.elasticsearch.core.ElasticsearchOperations' elasticsearchTemplate and found beans named elasticsearchTemplate
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate' in your configuration.
EsConfig.java
@Configuration(proxyBeanMethods=false)
@EnableElasticsearchRepositories(basePackages = "com.rahul.es.api.repository")
@ComponentScan(basePackages = { "com.rahul.es.api.service" })
public class EsConfig {
@Bean
public RestHighLevelClient client() {
ClientConfiguration clientConfiguration
= ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchRestTemplate(client());
}
}
QueryDSLService
@Service
public class QueryDSLService {
@Autowired
private ElasticsearchRestTemplate template;
public List<Customer> searchMultipleField(String firstname, int age) {
QueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("firstname", firstname))
.must(QueryBuilders.matchQuery("age", age));
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withQuery(query).build();
List<Customer> customers = template.queryForList(nativeSearchQuery, Customer.class);
return customers;
}
public List<Customer> getCustomerSearchData(String input) {
String search = ".*" + input + ".*";
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withFilter(QueryBuilders.regexpQuery("firstname", search)).build();
List<Customer> customers = template.queryForList(searchQuery, Customer.class);
return customers;
}
public List<Customer> multiMatchQuery(String text) {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.multiMatchQuery(text)
.field("firstname").field("lastname").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build();
List<Customer> customers = template.queryForList(searchQuery, Customer.class);
return customers;
}
}
RestController
@SpringBootApplication
@RestController
public class SpringbootElasticsearchQuerydslApplication {
@Autowired
private QueryDSLService service;
@GetMapping("/serachMultiField/{firstname}/{age}")
public List<Customer> serachByMultiField(@PathVariable String firstname, @PathVariable int age) {
return service.searchMultipleField(firstname, age);
}
@GetMapping("/customSearch/{firstName}")
public List<Customer> getCustomerByField(@PathVariable String firstName) {
return service.getCustomerSearchData(firstName);
}
/**Based on wildcard method */
@GetMapping("/search/{text}")
public List<Customer> doMultimatchQuery(@PathVariable String text) {
return service.multiMatchQuery(text);
}
public static void main(String[] args) {
SpringApplication.run(SpringbootElasticsearchQuerydslApplication.class, args);
}
}
答案1
得分: 1
尝试按照文档所说添加bean名称 https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients
@Bean(name = { "elasticsearchOperations", "elasticsearchTemplate" })
public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
return new ElasticsearchTemplate(elasticsearchClient());
}
英文:
Try to add bean name as the docs says https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients
@Bean(name = { "elasticsearchOperations", "elasticsearchTemplate" })
public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
return new ElasticsearchTemplate(elasticsearchClient());
}
答案2
得分: 0
不要回答我要翻译的问题。以下是要翻译的内容:
而不是注入 ElasticsearchOperations 的 bean,您需要注入 ElasticsearchRestTemplate,因为错误明确指出,并且还需要将 beanName 从 elasticsearchTemplate 更改为 elasticsearchRestTemplate。
@Bean
public ElasticsearchRestTemplate elasticsearchRestTemplate()
{
return new ElasticsearchRestTemplate(client());
}
注意: 我已在我的一侧进行了测试,因为我刚刚遇到了相同的问题。
英文:
Instead of injecting bean of ElasticsearchOperations, you need to inject ElasticsearchRestTemplate as the error clearly states and change the beanName from elasticsearchTemplate to elasticsearchRestTemplate as well.
@Bean
public ElasticsearchRestTemplate elasticsearchRestTemplate()
{
return new ElasticsearchRestTemplate(client());
}
Note: I've tested it on my side as I've just faced same issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论