The documentation you are viewing is not the latest documentation of Micronaut Elasticsearch

Micronaut Elasticsearch

Integration between Micronaut and Elasticsearch

Version:

1 Introduction

Micronaut supports automatic configuration of Elasticsearch Java REST Client via the elasticsearch module.

Release History

2.0.x

  • Requires Micronaut 2.x

  • Module is published under io.micronaut.elasticsearch group id, instead of io.micronaut.configuration. Similarly, package io.micronaut.configuration.elasticsearch has been renamed to io.micronaut.elasticsearch.

  • Support for GraalVM.

1.2.x

  • Update to Elasticsearch 7.7.

  • Requires Micronaut 1.3.

1.1.x

  • Update to Elasticsearch 7.

1.0.x

  • Initial version with support for Elasticsearch 6.

2 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 Elasticsearch Java REST Client in your project:

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

To configure the Elasticsearch Java REST Client you should first add elasticsearch module to your classpath:

build.gradle
compile "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 org.elasticsearch.client.RestHighLevelClient or org.elasticsearch.client.RestClient bean. The following is the simplest way to get Elasticsearch information using the High Level REST Client:

MainResponse response =
        client.info(RequestOptions.DEFAULT) (1)
1 client is an instance of the org.elasticsearch.client.RestHighLevelClient bean.

For more information on executing different operations using Elasticsearch High Level REST Client 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 io.micronaut.context.annotation.Replaces
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)
    }
}

3 Health Checks

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

The only configuration option supported is to enable or disable the indicator.

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

See the section on the Health Endpoint for more information.

4 GraalVM Support

Micronaut Elasticsearch is compatible with GraalVM out of the box. The only thing needed is to add some dependencies to make logging work.

To enable the logging when running on the JVM:

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>

Additional dependencies for logging on GraalVM:

implementation("org.apache.logging.log4j:log4j-api:2.13.3")
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.13.3</version>
</dependency>

implementation("org.apache.logging.log4j:log4j-core:2.13.3")
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.13.3</version>
</dependency>

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

5 Repository

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