获取以下错误:
应用程序启动失败
说明:
com.rahul.es.api.service.querydslservice中的字段模板需要找不到类型为“org.springframework.data.elasticsearch.core.elasticsearchrestemplate”的bean。
注入点具有以下注解:-@org.springframework.beans.factory.annotation.autowired(required=true)
已找到以下候选对象,但无法注入:“'elasticsearchdataconfiguration.restclientconfiguration'中的bean方法'elasticsearchtemplate'未加载,因为@conditionalonmissingbean(名称:elasticsearchtemplate types:org.springframework.data.elasticsearch.core.elasticsearchoperations;searchstrategy:all)找到了“org.springframework.data.elasticsearch.core.elasticsearchoperations”elasticsearchtemplate类型的bean,并找到了名为elasticsearchtemplate的bean
行动:
考虑重新访问上面的条目或在配置中定义类型为“org.springframework.data.elasticsearch.core.elasticsearchrestemplate”的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());
}
}
查询服务
@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;
}
}
REST控制器
@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);
}
}
2条答案
按热度按时间5hcedyr01#
与注入elasticsearchoperations的bean不同,您需要注入elasticsearchrestemplate,因为错误清楚地表明了这一点,并将beanname从elasticsearchtemplate更改为elasticsearchrestemplate。
注意:我已经在我这边测试过了,因为我刚刚遇到了同样的问题。
0s7z1bwu2#
试着像文档中说的那样添加bean名称https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients