Micronaut Elasticsearch

Integration between Micronaut and Elasticsearch

Version: 5.6.0

1 Introduction

Micronaut supports automatic configuration of Elasticsearch ElasticsearchClient via the elasticsearch module.

2 Release History

For this project, you can find a list of releases (with release notes) here:

3 Configuration

Configuring the Elasticsearch Client

Using the CLI

If you are creating your project using the Micronaut CLI, supply the elasticsearch feature to configure the ElasticsearchClient in your project:

$ mn create-app my-app --features elasticsearch

To configure the ElasticsearchClient you should first add elasticsearch module to your classpath:

build.gradle
implementation("io.micronaut.elasticsearch:micronaut-elasticsearch")

You should then configure the httpHosts of the Elasticsearch server you wish to communicate with in application.yml as:

application.yml
elasticsearch:
  httpHosts: "http://localhost:9200,http://127.0.0.2:9200"

See the API for DefaultElasticsearchConfigurationProperties for more information on the available configuration options.

Once you have the above configuration in place then you can inject the co.elastic.clients.elasticsearch.ElasticsearchClient, the co.elastic.clients.elasticsearch.ElasticsearchAsyncClient or the org.elasticsearch.client.RestClient bean. The following is the simplest way to get Elasticsearch information using the ElasticsearchClient:

InfoResponse response =
        client.info() (1)
1 client is an instance of the co.elastic.clients.elasticsearch.ElasticsearchClient bean.

For more information on executing different operations using ElasticsearchClient please read the Elasticsearch Documentation.

Modify the Default Request Configurations

Often times you want to change the default configurations. To achieve this Micronaut, includes the ability to change the default request configurations. You can set the default request configurations under key elasticsearch.request.default as:

application.yml
elasticsearch:
  httpHosts: http://localhost:9200,http://127.0.0.2:9200
  request:
    default:
      expectContinueEnabled: true
      localAddress: 198.57.151.22

See the API RequestConfig.Builder for more information on the available configuration options.

Modify the HTTP Client Configurations

To modify the HTTP Client configurations (e.g. request timeouts, authentication, or anything that the HttpAsyncClientBuilder allows to set). You can define a bean using Factory which replaces org.apache.http.impl.nio.client.HttpAsyncClientBuilder.

Following is an example to change the default credentials provider:

import jakarta.inject.Singleton
import org.apache.http.auth.AuthScope
@Factory
static class HttpAsyncClientBuilderFactory {

    @Replaces(HttpAsyncClientBuilder.class)
    @Singleton
    HttpAsyncClientBuilder builder() {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider()
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("user", "password"))

        return HttpAsyncClientBuilder.create()
                .setDefaultCredentialsProvider(credentialsProvider)
    }
}

Logging

Elasticsearch uses log4j for logging but Micronaut uses slf4j with Logback. To make it work properly, add the following dependency:

runtimeOnly("org.slf4j:log4j-over-slf4j:1.7.30")
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>1.7.30</version>
    <scope>runtime</scope>
</dependency>

4 Health Checks

When the elasticsearch module is activated a ElasticsearchClientHealthIndicator is activated resulting in the /health endpoint and CurrentHealthStatus interface resolving the health of the Elasticsearch cluster.

To enable or disable the indicator use:

application.yml
endpoints:
  health:
    elasticsearch:
      enabled: true

To use the deprecated health indicator use:

application.yml
endpoints:
  health:
    elasticsearch:
      rest:
        high:
          level:
            enabled: false

See the section on the Health Endpoint for more information.

5 GraalVM Support

Micronaut Elasticsearch is compatible with GraalVM out of the box.

See the section on GraalVM in the user guide for more information.

6 Repository

You can find the source code of this project in this repository: