Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring data JPA & spring data elasticsearch; No property index found for type?

I'm unsure why this is happening! I've got a class that is used by spring data elasticsearch and spring data jpa, but when I try run my application I get an error.

Error creating bean with name 'articleSearch': 
Invocation of init method failed; nested exception is 
org.springframework.data.mapping.PropertyReferenceException: 
No property index found for type Article!

Caused by: org.springframework.data.mapping.PropertyReferenceException: 
No property index found for type Article!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.11.4.RELEASE.jar:na]

I've got the following application class:

@SpringBootApplication
@EnableAsync
@ComponentScan(basePackages = {"com.article.models", "com.user"})
public class ArticleApplication {

And the following elasticsearch config:

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.article.search")
public class ElasticSearchConfiguration {
    @Resource
    private Environment environment;

    @Bean
    public Client client() {
        TransportClient client = new TransportClient();
        TransportAddress address = new InetSocketTransportAddress(environment.getProperty("elasticsearch.host"), Integer.parseInt(environment.getProperty("elasticsearch.port")));
        client.addTransportAddress(address);
        return client;
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(client());
    }
}

This is how I've setup my model class:

@Entity
@Table(name="article")
@Document(indexName="article", type="articles")

public class Article implements Serializable {

I've then got a package search that extends the elasticsearchrepository, like so:

public interface ArticleSearch extends ElasticsearchRepository<Article, String> {

I'm trying to autowire the articlesearch class inside another service which is causing the error to occur:

@Autowired
ArticleSearch articleSearch;

What am I missing here?! I guess it's a bit more complex when trying to use data-jpa + data-elasticsearch.

like image 845
James111 Avatar asked Aug 27 '16 02:08

James111


1 Answers

I found out why this was happening. I'm not sure why, but spring didn't seem to be picking up my ElasticSearchConfiguration configuration class!

So I simply moved all the contents from that and dumped it in my main application class (where all my other config is).

I also removed component scan & added the enablejparepository + enableelasticsearchrepository annotations to my main class. Here is what it looks like now:

@SpringBootApplication
@EnableAsync
@EnableElasticsearchRepositories(basePackages = "com.article.search")
@EnableJpaRepositories(basePackages = {"com.article.dao", "com.user.dao"})
public class ArticleApplication {
like image 194
James111 Avatar answered Nov 15 '22 09:11

James111