This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Data Meilisearch 0.8.1!

Meilisearch Repositories

This chapter describes how to use Meilisearch repositories.

Usage

Meilisearch repositories are used to store and retrieve data from Meilisearch.

Example 1. The sample repository
import io.vanslog.spring.data.meilisearch.repository.MeilisearchRepository;

import java.util.List;

public class MovieService {

	private final MovieRepository repository;

	public MovieService(MovieRepository repository) {
		this.repository = repository;
	}

	public List<Movie> showAllMovies() {
		return repository.findAll();
	}
}

public interface MovieRepository extends MeilisearchRepository<Movie, String> {

}

Automatic creation of indexes with the corresponding mapping

If the @Document annotation is present on the entity, the index will be created automatically with the corresponding mapping.

Lookup methods

The Meilisearch repository provides the following lookup methods:

  • findById(String id)

  • findAllById(Iterable<String> ids)

  • findAll()

  • findAll(Sort sort)

  • findAll(Pageable pageable)

Note that the above methods perform different behaviors. The findById and findAllById methods use the Get one document or Get documents API. However, the findAll method uses the Search API.

For findAll(Pageable pageable), the returned Page total elements come from Meilisearch paginated search totalHits. The repository does not use estimated totals or a separate count() query for page totals. The number of elements in the page content remains the number of hits returned for the requested page. If the entity configures @Pagination(maxTotalHits = …​), Meilisearch can cap the reported totalHits, and the repository Page total reflects that capped value.

This difference in behavior can also cause results to show differently. For example, displayedAttributes in Meilisearch Settings does not work with the Get one document or Get documents API. Go to Meilisearch documentation for more information.

Annotation based configuration

The Spring Data Meilisearch repositories can be configured using the @EnableMeilisearchRepositories annotation.

Example 2. Spring Data Meilisearch repositories using JavaConfig
import io.vanslog.spring.data.meilisearch.core.MeilisearchOperations;
import io.vanslog.spring.data.meilisearch.repository.config.EnableMeilisearchRepositories;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableMeilisearchRepositories(                             (1)
		basePackages = {"com.example.repositories"}
)
public class Config {

    @Bean
    public MeilisearchOperations meilisearchTemplate() {    (2)
        // ...
    }
}
1 The @EnableMeilisearchRepositories annotation enables Meilisearch repositories. If no basePackages are configured, the annotation will scan the package of the annotated configuration class.
2 Provide a MeilisearchOperations bean to override the default MeilisearchTemplate bean.

Spring Namespace

The Spring Data Meilisearch repositories can be configured using the meilisearch namespace.

Example 3. Spring Data Meilisearch repositories using Namespace
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:meilisearch="http://www.vanslog.io/spring/data/meilisearch"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.vanslog.io/spring/data/meilisearch
       http://www.vanslog.io/spring/data/meilisearch/spring-meilisearch-1.0.xsd">

    <meilisearch:repositories base-package="com.example.repositories"/>             (1)

    <bean name="meilisearchTemplate"                                                (2)
          class="io.vanslog.spring.data.meilisearch.core.MeilisearchTemplate">
        <constructor-arg name="meilisearchClient" ref="meilisearchClient"/>         (3)
    </bean>

    <meilisearch:meilisearch-client id="meilisearchClient" api-key="masterKey"/>    (4)
    <bean id="jsonHandler" class="com.meilisearch.sdk.json.GsonJsonHandler"/>       (5)
</beans>
1 The meilisearch:repositories element enables Meilisearch repositories. If no base-package is configured, the namespace will scan the package of the configuration file.
2 The meilisearchTemplate bean must be configured with a MeilisearchClient.
3 Set the client bean as the constructor argument of the meilisearchTemplate.
4 Configure the Meilisearch client with the api-key attribute.
5 Configure the jsonHandler bean with the GsonJsonHandler class.