Elasticsearch container

This module helps running elasticsearch using Testcontainers.

Note that it's based on the official Docker image provided by elastic.

Usage example

You can start an elasticsearch container instance from any Java application by using:

        // Create the elasticsearch container.
        try (ElasticsearchContainer container = new ElasticsearchContainer()) {
            // Start the container. This step might take some time...
            container.start();

            // Do whatever you want with the rest client ...
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD));

            client = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
                .build();

            Response response = client.performRequest(new Request("GET", "/_cluster/health"));



        }
        // Create the elasticsearch container.
        try (ElasticsearchContainer container = new ElasticsearchContainer()) {
            // Start the container. This step might take some time...
            container.start();

            // Do whatever you want with the transport client
            TransportAddress transportAddress = new TransportAddress(container.getTcpHost());
            String expectedClusterName = "docker-cluster";
            Settings settings = Settings.builder().put("cluster.name", expectedClusterName).build();
            try (TransportClient transportClient = new PreBuiltTransportClient(settings)
                .addTransportAddress(transportAddress)) {
                ClusterHealthResponse healths = transportClient.admin().cluster().prepareHealth().get();
                String clusterName = healths.getClusterName();



            }
        }

Note that if you are still using the TransportClient (not recommended as it is deprecated), the default cluster name is set to docker-cluster so you need to change cluster.name setting or set client.transport.ignore_cluster_name to true.

Choose your Elasticsearch license

If you prefer to start a Docker image with the pure OSS version (which means with no security in older versions or other new and advanced features), you can use this instead:

ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:" + ELASTICSEARCH_VERSION)

Adding this module to your project dependencies

Add the following dependency to your pom.xml/build.gradle file:

testCompile "org.testcontainers:elasticsearch:1.14.3"
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>1.14.3</version>
    <scope>test</scope>
</dependency>