|
For the latest stable version, please use Spring Data Meilisearch 0.12.0! |
Defining Repository Interfaces
Spring Data repository interfaces are typed to the domain class and its identifier type. For Spring Data Meilisearch, the clearest way to declare a repository is to extend MeilisearchRepository<T, ID>.
Selecting Repository Methods
MeilisearchRepository<T, ID> combines the blocking Spring Data CrudRepository and PagingAndSortingRepository contracts. Its built-in CRUD, sorting, and paging methods are implemented by SimpleMeilisearchRepository.
import io.vanslog.spring.data.meilisearch.annotations.Document;
import io.vanslog.spring.data.meilisearch.repository.MeilisearchRepository;
import org.springframework.data.annotation.Id;
@Document(indexUid = "movies")
class Movie {
@Id String id;
}
interface MovieRepository extends MeilisearchRepository<Movie, String> {
}
The module supports blocking repository interfaces only. Reactive, RxJava, and Kotlin coroutine repository interfaces are not supported.
You can expose a smaller subset of the standard methods by extending the generic Spring Data Repository marker or by using @RepositoryDefinition. Declare only method signatures implemented by the repository base class:
import java.util.Optional;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
@NoRepositoryBean
interface IdentifiedRepository<T, ID> extends Repository<T, ID> {
Optional<T> findById(ID id);
<S extends T> S save(S entity);
}
interface MovieRepository extends IdentifiedRepository<Movie, String> {
}
In a multi-module application, annotate the domain type with Meilisearch’s @Document when the repository does not extend MeilisearchRepository, so strict repository configuration can identify it. In a single-module application, the generic repository may be discovered in the configured package, but extending MeilisearchRepository makes the store binding explicit. Mark generic intermediate interfaces with @NoRepositoryBean so Spring does not try to instantiate them directly.
The method declarations above match built-in CRUD methods. Adding a derived finder such as findByTitle(…), a repository @Query method, or a named-query method is not supported and causes repository bootstrap to fail. Use MeilisearchOperations for application-specific searches; see Repository Methods and Limitations.
Using Repositories with Multiple Spring Data Modules
When more than one Spring Data module is on the classpath, Spring Data uses strict repository configuration to decide which module should create each repository. Meilisearch identifies candidates through either the MeilisearchRepository interface or the Meilisearch @Document annotation on the domain type.
import io.vanslog.spring.data.meilisearch.annotations.Document;
import io.vanslog.spring.data.meilisearch.repository.MeilisearchRepository;
import org.springframework.data.annotation.Id;
@Document(indexUid = "movies")
class Movie {
@Id String id;
}
interface MovieRepository extends MeilisearchRepository<Movie, String> {
}
Extending MeilisearchRepository identifies this interface as a Meilisearch repository.
import io.vanslog.spring.data.meilisearch.annotations.Document;
import org.springframework.data.annotation.Id;
import org.springframework.data.repository.Repository;
@Document(indexUid = "movies")
class Movie {
@Id String id;
}
interface MovieRepository extends Repository<Movie, String> {
}
The Meilisearch @Document annotation identifies the domain type when the repository interface uses only the generic Repository marker. Use io.vanslog.spring.data.meilisearch.annotations.Document; other Spring Data modules may define a different annotation with the same simple name.
A generic repository interface without a Meilisearch-specific parent or a Meilisearch @Document domain type cannot be identified as a Meilisearch repository in strict configuration. Avoid relying on a domain type carrying multiple modules' identifying annotations to select a unique store. You can also use separate scan packages to keep repository candidates distinct; see Creating Repository Instances.