1

I've been trying to connect to my ES instance on the Elastic cloud with no success using the Java client and following the Documentation In the hostname I put https://myinstance-xx.europe-west1.gcp.cloud.es.io

private String hostName = "https://myinstance-xx.es.europe-west1.gcp.cloud.es.io";
private String username = "username";
private String password = "pass";
@Bean
public ElasticsearchClient client() {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(username, password));
    RestClientBuilder builder = RestClient.builder(new HttpHost(hostName, 9200))
            .setHttpClientConfigCallback(httpClientBuilder ->
                    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    // Create the low-level client
    RestClient restClient = builder.build();
    // Create the transport with a Jackson mapper
    ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
    // And create the API client
    return new ElasticsearchClient(transport);
}

However, I get an Connection Refused exception

java.net.ConnectException: Connection refused

What's the best way to connect to ES on Elastic Cloud through the Java Client?

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
Ala Abid
  • 2,256
  • 23
  • 35
  • check https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.5/_encrypted_communication.html as you are using https – P.J.Meisch Nov 07 '22 at 18:42

1 Answers1

2
  1. Go to cloud.elastic.go/home after your are logged in, find the "Manage this deployment" link. It should be something like https://cloud.elastic.co/deployments/xxxxxxxx
  2. Under "Applications" you should have an "Elasticsearch" enpoint (click on "Copy endpoint")
  3. On the API console create an API Key:
POST /_security/api_key 
{
  "name": "my_key_name",
  "expiration": "15d"
}

That's gonna give you something like:

{
  "id": "API_KEY_ID",
  "name": "my_key_name",
  "expiration": 1670471984009,
  "api_key": "API_KEY",
  "encoded": "XxxxxxxxXXXXXxxxxxxXXXXxxxxxXXXX=="
}
  1. Now in your Java code:

    // endpoint you copied
    String hostname = "youdepname.es.us-west1.gcp.cloud.es.io";
    String apiKeyId = "API_KEY_ID";
    String apiKeySecret = "API_KEY";
    String apiKeyAuth =
         Base64.getEncoder().encodeToString(
             (apiKeyId + ":" + apiKeySecret)
                 .getBytes(StandardCharsets.UTF_8));
    
    
    RestClientBuilder builder = RestClient.builder(
         new HttpHost(hostname, 9243, "https"))
         .setRequestConfigCallback(
             new RestClientBuilder.RequestConfigCallback() {
               @Override
               public RequestConfig.Builder customizeRequestConfig(
                   RequestConfig.Builder requestConfigBuilder) {
                 return requestConfigBuilder
                     .setConnectTimeout(5000)
                     .setSocketTimeout(60000);
               }
             });
    
    
    Header[] defaultHeaders =
         new Header[]{new BasicHeader("Authorization",
             "ApiKey " + apiKeyAuth)};
    builder.setDefaultHeaders(defaultHeaders);
    
    RestClient restClient = builder.build();
    
    // Create the transport with a Jackson mapper
    ElasticsearchTransport transport = new RestClientTransport(
         restClient, new JacksonJsonpMapper());
    
    // And create the API client
    ElasticsearchClient client = new ElasticsearchClient(transport);
    

That should do it. There are other ways to connect, but this is my preferred way.

BSB
  • 1,516
  • 13
  • 14