0

Below are the maven version we are using

 <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>5.6.13</version>
  </dependency>


    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>5.6.13</version>
    </dependency>



 private RestHighLevelClient buildRestClient(ElasticRequestVO elasticRequestVO) {
    String elasticHost =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticHost()) ? elasticRequestVO.getElasticHost()
                    : elasticSearchHost;
    int elasticPort =
            (elasticRequestVO.getElasticPort() != 0) ? elasticRequestVO.getElasticPort() : elasticSearchPort;
    String elasticUser =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticUser()) ? elasticRequestVO.getElasticUser()
                    : elasticSearchUser;
    String elasticPassword =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticPassword()) ? elasticRequestVO.getElasticPassword()
                    : elasticSearchPassword;
    HttpHost host = new HttpHost(elasticHost, elasticPort);
    RestClientBuilder restClientBuilder = RestClient.builder(host);

    Optional<String> encodedAuth = getAuthenticationHeader(elasticUser, elasticPassword);

    if (encodedAuth.isPresent()) {
        Header[] requestHeaders =
                new Header[] {new BasicHeader(org.apache.http.HttpHeaders.AUTHORIZATION, encodedAuth.get())};
        restClientBuilder.setDefaultHeaders(requestHeaders);
    }
    return new RestHighLevelClient(restClientBuilder.build());
}

How to close RestHighLevelClient?

Ramesh Papaganti
  • 7,311
  • 3
  • 31
  • 36

4 Answers4

1

If you will see how closing is implemented in one of the latest versions, e.g. 6.2.4 you will found that the RestHighLevelClient just releases the RestClient.
So for closing I can offer you just invoke the restHighLevelClient.getLowLevelClient() and close the rest client lowLevelRestClient.close().

P.S. If just look at the source code would notice that this is just a wrapper over the rest client.

xxxception
  • 915
  • 10
  • 6
  • No getLowLevelClient for old version : https://artifacts.elastic.co/javadoc/org/elasticsearch/client/elasticsearch-rest-high-level-client/5.6.0/org/elasticsearch/client/RestHighLevelClient.html – Thierry Sep 07 '20 at 16:30
1

Insted of returning RestHighLevelClient, retun RestClient from buildRestClient()

Here if code sample

private RestClient buildRestClient(ElasticRequestVO elasticRequestVO) {
    String elasticHost =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticHost()) ? elasticRequestVO.getElasticHost()
                    : elasticSearchHost;
    int elasticPort =
            (elasticRequestVO.getElasticPort() != 0) ? elasticRequestVO.getElasticPort() : elasticSearchPort;
    String elasticUser =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticUser()) ? elasticRequestVO.getElasticUser()
                    : elasticSearchUser;
    String elasticPassword =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticPassword()) ? elasticRequestVO.getElasticPassword()
                    : elasticSearchPassword;
    HttpHost host = new HttpHost(elasticHost, elasticPort);
    RestClientBuilder restClientBuilder = RestClient.builder(host);

    Optional<String> encodedAuth = getAuthenticationHeader(elasticUser, elasticPassword);

    if (encodedAuth.isPresent()) {
        Header[] requestHeaders =
                new Header[] {new BasicHeader(org.apache.http.HttpHeaders.AUTHORIZATION, encodedAuth.get())};
        restClientBuilder.setDefaultHeaders(requestHeaders);
    }
    return restClientBuilder.build();
}
Ramesh Papaganti
  • 7,311
  • 3
  • 31
  • 36
0

The version of my RestHighLevelClient is 5.6.3,and I can't find method like restHighLevelClient.getLowLevelClient() or restHighLevelClient.close(),so I use reflection to get the restClient to close

Field restClientField = RestHighLevelClient.class.getDeclaredField("client");
restClientField.setAccessible(true);
RestClient restclient = (RestClient)restClientField.get(restHighLevelClient);
restClient.close();
chick
  • 1
0

I have the same problem. To close the connexion you must change your method :

private RestClient buildRestClient(ElasticRequestVO elasticRequestVO) {
    String elasticHost =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticHost()) ? elasticRequestVO.getElasticHost()
                    : elasticSearchHost;
    int elasticPort =
            (elasticRequestVO.getElasticPort() != 0) ? elasticRequestVO.getElasticPort() : elasticSearchPort;
    String elasticUser =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticUser()) ? elasticRequestVO.getElasticUser()
                    : elasticSearchUser;
    String elasticPassword =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticPassword()) ? elasticRequestVO.getElasticPassword()
                    : elasticSearchPassword;
    HttpHost host = new HttpHost(elasticHost, elasticPort);
    RestClientBuilder restClientBuilder = RestClient.builder(host);

    Optional<String> encodedAuth = getAuthenticationHeader(elasticUser, elasticPassword);

    if (encodedAuth.isPresent()) {
        Header[] requestHeaders =
                new Header[] {new BasicHeader(org.apache.http.HttpHeaders.AUTHORIZATION, encodedAuth.get())};
        restClientBuilder.setDefaultHeaders(requestHeaders);
    }
    return restClientBuilder.build();
}

Then, when you call your method you can work with

final RestClient restClient=buildRestClient(...); 

and do

new RestHighLevelClient(restClient) 

when needed. At the end, you close.

Exemple

try (final RestClient restClient=buildRestClient(...)) {
final RestHighLevelClient restHLClient = new RestHighLevelClient(restClient);
// what you want
} 
Thierry
  • 89
  • 1
  • 5