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 Client

This chapter describes configuration and usage of the Meilisearch client.

Spring Data Meilisearch operates upon a Meilisearch Client (provided by Meilisearch Java) that is connected to a single Meilisearch instance.

Annotation based configuration

The Spring Data Meilisearch client can be configured using the @Configuration annotation.

Example 1. Spring Data Meilisearch client using JavaConfig
import io.vanslog.spring.data.meilisearch.client.ClientConfiguration;
import io.vanslog.spring.data.meilisearch.config.MeilisearchConfiguration;

import org.springframework.context.annotation.Configuration;

@Configuration
public class MyClientConfig extends MeilisearchConfiguration {

    @Override
    public ClientConfiguration clientConfiguration() {
        return ClientConfiguration.builder()           (1)
                .connectedToLocalhost()
                .withApiKey("masterKey")
                .build();
    }
}
1 For a detailed description of the builder methods see Client Configuration.

Spring Namespace

The Spring Data Meilisearch client can be configured using the Spring XML namespace.

Example 2. Spring Data Meilisearch client using XML 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:meilisearch-client id="meilisearchClient" api-key="masterKey"/>

</beans>
When setting up a client using the Spring namespace, a JSON handler must be set up. Use the JSON handler namespace configuration.

Client Configuration

Configuration options are provided by the ClientConfiguration builder.

Example 3. Client Configuration
String[] agents = {"Spring Data Meilisearch"};                          (1)

ClientConfiguration clientConfiguration = ClientConfiguration.builder()
    .connectedTo("http://localhost:7700")                               (2)
    .withApiKey("masterKey")                                            (3)
    .withClientAgents(agents)                                           (4)
    .withRequestTimeout(2000)                                           (5)
    .withRequestInterval(20)                                            (6)
    .build();
1 Define the client agents that will be sent as a User-Agent header to Meilisearch.
2 Use the builder to provide the Meilisearch host URL.
3 Set the Meilisearch API Key to use for authentication.
4 Set the client agents.
5 Set the request timeout to 2000 milliseconds.
6 Set the request interval to 20 milliseconds.

JSON handler

The JSON handler is used to serialize and deserialize the JSON data.

Supported JSON handlers

The following JSON handlers are supported:

  • com.meilisearch.sdk.json.GsonJsonHandler

  • com.meilisearch.sdk.json.JacksonJsonHandler

  • Custom JSON handler that implements com.meilisearch.sdk.json.JsonHandler

Annotation based configuration

You don’t need to provide a JSON handler if you are using the default JSON handler, GsonJsonHandler. But you can override the jsonHandler() method to provide a custom JSON handler.

Example 4. Setting JSON handler using JavaConfig
import io.vanslog.spring.data.meilisearch.config.MeilisearchConfiguration;

import com.meilisearch.sdk.json.JacksonJsonHandler;
import com.meilisearch.sdk.json.JsonHandler;

import org.springframework.context.annotation.Configuration;

@Configuration
public class JsonHandlerConfig extends MeilisearchConfiguration {

    @Override
    public JsonHandler jsonHandler() {
        return new JacksonJsonHandler();    (1)
    }
}
1 Use Jackson JSON handler instead of the default JSON handler.

Spring Namespace

Register a JSON handler bean and reference it from <meilisearch:meilisearch-client> using json-handler-ref. If json-handler-ref is omitted, the default bean name is jsonHandler.

Example 5. Setting JSON handler using XML 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">

    <bean id="jsonHandler" class="com.meilisearch.sdk.json.GsonJsonHandler"/>

    <meilisearch:meilisearch-client
        id="meilisearchClient"
        api-key="masterKey"
        json-handler-ref="jsonHandler"/>

</beans>