Connecting to ElasticSearch Cloud 5.x with Java Client and X-Pack/HTTPS Connecting to ElasticSearch Cloud 5.x with Java Client and X-Pack/HTTPS elasticsearch elasticsearch

Connecting to ElasticSearch Cloud 5.x with Java Client and X-Pack/HTTPS


After some discussion with people on ES forum, I got it working finally. Here are some tips when you run into an issue with connection:

1) You probably didn't add this in the header since it's not mentioned in the documentation

.put("request.headers.X-Found-Cluster", clusterName)

2) If that still doesn't work, use the sample code from here to verify that you hostname is correct and you have the correct credential

https://github.com/elastic/found-shield-example#running

3) If you are trying to run the sample code within Eclipse, replace ${cluster.name} before you run it or it won't work.

Hope this help save people some time trying to figure out connection issue. Connecting to ES 5.x is about as easy as 2.x. The problem is that the documentation is not clear and there is no link to sample client.


I had the same issue before and I resolved it using the following configuration

Required dependencies:

(transport and x-pack-transport should have the same version of the elasticsearch 5.5.3)

<dependencies>    <dependency>        <groupId>org.springframework.data</groupId>        <artifactId>spring-data-elasticsearch</artifactId>        <version>3.0.5.RELEASE</version>    </dependency>    <dependency>        <groupId>org.elasticsearch.client</groupId>        <artifactId>transport</artifactId>        <version>5.5.3</version>    </dependency>    <!--add the x-pack jar as a dependency-->    <dependency>        <groupId>org.elasticsearch.client</groupId>        <artifactId>x-pack-transport</artifactId>        <version>5.5.3</version>    </dependency></dependencies><repositories>    <!-- add the elasticsearch repo -->    <repository>        <id>elasticsearch-releases</id>        <url>https://artifacts.elastic.co/maven</url>        <releases>            <enabled>true</enabled>        </releases>        <snapshots>            <enabled>false</enabled>        </snapshots>    </repository></repositories>

Java Config:

@Value("${elasticsearch.cluster.url}")private String elasticsearchClusterUrl;@Value("${elasticsearch.cluster.port}")private int elasticsearchClusterPort;@Value("${elasticsearch.cluster.name}")private String elasticsearchClusterName;@Value("${elasticsearch.cluster.credentials}")private String elasticsearchCredentials;@Beanpublic TransportClient elasticsearchClient()        throws Exception {    Settings settings = Settings.builder()            .put("cluster.name", elasticsearchClusterName)            .put("request.headers.X-Found-Cluster", elasticsearchClusterName)            .put("xpack.security.transport.ssl.enabled", true)            .put("xpack.security.user", elasticsearchCredentials)            .build();    return new PreBuiltXPackTransportClient(settings)            .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(elasticsearchClusterUrl, elasticsearchClusterPort)));}

application.yml:

elasticsearch:  index: dev_index  cluster:    name: 2083ddf0fbf5a3b1f0c50ff257ded077    url: 2083ddf0fbf5a3b1f0c50ff257ded077.eu-west-1.aws.found.io    port: 9343    credentials: elastic:pass