0

在我的Spring Boot应用程序中,我试图通过Elasticsearch实现模糊搜索。Spring Data Elasticsearch Cusom知识库

这是我的ES配置:

@Profile("test") 
@Configuration 
@EnableElasticsearchRepositories(basePackages = "com.example.domain.repository.elasticsearch") 
public class ElasticsearchTestConfig { 
} 

我有一个存储库:

@Repository 
public interface ESDecisionRepository extends ElasticsearchRepository<ESDecision, String>, ESDecisionRepositoryCustom { 
} 

为了能够做我创建一个自定义库中的模糊搜索:

public interface ESDecisionRepositoryCustom { 

    public List<ESDecision> findFuzzyBySearchTerm(String searchTerm); 

} 

并提供了一个自定义实现:

@Repository 
public class ESDecisionRepositoryCustomImpl implements ESDecisionRepositoryCustom { 

    @Autowired 
    protected ElasticsearchTemplate elasticsearchTemplate; 

    @Override 
    public List<ESDecision> findFuzzyBySearchTerm(String searchTerm) { 
     Criteria c = new Criteria("name").fuzzy(searchTerm); 
     return elasticsearchTemplate.queryForList(new CriteriaQuery(c), ESDecision.class); 
    } 

} 

现在启动我的应用程序时将失败,以下情况除外:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ESDecisionRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property searchTerm found for type ESDecision! 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) 
    ... 43 common frames omitted 
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property searchTerm found for type ESDecision! 
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) 
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) 
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:247) 
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398) 
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:378) 
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:89) 
    at org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery.<init>(ElasticsearchPartQuery.java:44) 
    at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory$ElasticsearchQueryLookupStrategy.resolveQuery(ElasticsearchRepositoryFactory.java:119) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) 
    at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean.afterPropertiesSet(ElasticsearchRepositoryFactoryBean.java:67) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) 
    ... 53 common frames omitted 

什么我做错了,如何解决呢?

+0

你在'ESDecision'实体类中有属性'searchTerm'吗? –

+0

不,我没有这样的属性 – alexanoid

+1

尝试更改您的自定义回购实现名称为'ESDecisionRepositoryImpl'。自定义存储库实现有一个命名约定,您需要按照它来使其工作。查看[docs](http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behaviour) –

回答

1

将您的自定义存储库实现类名称从ESDecisionRepositoryCustomImpl更改为ESDecisionRepositoryImpl

从文档

被发现的类最重要的位是名称的后缀默认地将Impl它比核心库接口(见下文)。

有一个命名约定必须遵循才能使自定义存储库实现工作。退房docs

试试这个:

@Repository 
public class ESDecisionRepositoryCustomImpl implements ESDecisionRepositoryCustom { 

    @Autowired 
    protected ElasticsearchTemplate elasticsearchTemplate; 

    @Override 
    public List<ESDecision> findFuzzyBySearchTerm(String searchTerm) { 
     Criteria c = new Criteria("name").fuzzy(searchTerm); 
     return elasticsearchTemplate.queryForList(new CriteriaQuery(c), ESDecision.class); 
    } 

} 

希望这有助于。

相关问题