$ mn create-app my-app --features elasticsearch
Elasticsearch configuration
Configuration to integrate Micronaut and Elasticsearch
Version: 1.0.0
1 Introduction
Micronaut supports automatic configuration of Elasticsearch Java REST Client via the elasticsearch
module.
2 Configuration
Configuring the Elasticsearch Client
Using the CLI
If you are creating your project using the Micronaut CLI, supply the |
To configure the Elasticsearch Java REST Client you should first add elasticsearch
module to your classpath:
compile "io.micronaut.configuration:micronaut-elasticsearch"
You should then configure the httpHosts
of the Elasticsearch server you wish to communicate with in application.yml
as:
elasticsearch:
httpHosts: "http://localhost:9200,http://127.0.0.2:9200"
See the API for api:configuration.mongo.reactive.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:
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 api:io.micronaut.context.annotation.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 api:configuration.elasticsearch.health.ElasticsearchHealthIndicator[] is activated resulting in the /health
endpoint and api:health.CurrentHealthStatus[] interface resolving the health of the Elasticsearch cluster.
The only configuration option supported is to enable or disable the indicator by the endpoints.health.elasticsearch.enabled
key.
See the section on the Health Endpoint for more information.