Mauro Monti
Mauro Monti

Reputation: 1088

Spring Boot + Elastic Search

I am trying to setup an application with Spring Boot and Elastic Search. This application already use Spring Data JPA repositories to persist my entities. The problem that I have when I try to run the application with the ElasticSearch configuration enabled is that I am getting an exception when the repositories are scanned.

I am getting the following exception:

Caused by: java.lang.IllegalArgumentException: Unable to obtain mapping metadata for int!

My repository is defined in the following way:

@Repository
public interface AdminUserRepository extends PagingAndSortingRepository<AdminUser, Long> {

    /**
     * Returns an AdminUser that match the email specified by parameter.
     * @param email AdminUser email.
     * @return An AdminUser instance.
     */
    AdminUser findByEmail(final String email);

    /**
     * Returns an AdminUser that match the email and business name specified by parameter.
     * @param email AdminUser email.
     * @param businessName Business Name.
     * @return number of matching instances.
     */
    int countByEmailAndBusinessName(final String email, final String businessName);
}

Seems that the exception occurs due to the signature of the count query which returns an int. Even though this repository works fine with JPA, it throws an exception enabling elastic search.

I want to know if there is any restrictions with the return type in a repository or if I am missing something in my configuration.

My Java config class:

@Configuration
@EnableElasticsearchRepositories
public class ElasticSearchConfig {
}

Thanks!

Upvotes: 3

Views: 5518

Answers (2)

Alex Rashkov
Alex Rashkov

Reputation: 10005

For counts spring data uses Long not int. Try changing the type of the method that should work.

Here is a reference at the docs: http://docs.spring.io/autorepo/docs/spring-data-elasticsearch/2.0.2.RELEASE/reference/html/#repositories.core-concepts

Upvotes: 0

Andy Wilkinson
Andy Wilkinson

Reputation: 116051

It looks like Spring Data Elasticsearch is finding a repository intended for use with Spring Data JPA. When you're using multiple Spring Data modules in the same application, you should place the repositories in separate packages and then reference this package on the @Enable... annotation.

For example:

@Configuration
@EnableElasticsearchRepositories("com.foo.elasticsearch")
@EnableJpaRepositories("com.foo.jpa")
public class MyConfiguration {

}

Upvotes: 10

Related Questions