Changing the client’s initialization code

edit

Changing the client’s initialization code

edit

The TransportClient is typically initialized as follows:

Settings settings = Settings.builder()
        .put("cluster.name", "prod").build();

TransportClient transportClient = new PreBuiltTransportClient(settings)
        .addTransportAddress(new TransportAddress(InetAddress.getByName("host"), 9300));

The initialization of a RestHighLevelClient is different. It first requires the initialization of a low-level client:

RestClient lowLevelRestClient = RestClient.builder(
                new HttpHost("host", 9200, "http")).build();

The RestClient uses Elasticsearch’s HTTP service which is bounded by default on 9200. This port is different from the port used to connect to Elasticsearch with a TransportClient.

Which is then passed to the constructor of the RestHighLevelClient:

RestHighLevelClient client =
    new RestHighLevelClient(lowLevelRestClient);

Both RestClient and RestHighLevelClient are thread safe. They are typically instantiated by the application at startup time or when the first request is executed.

Once the RestHighLevelClient is initialized, it can then be used to execute any of the supported APIs.

As with the TransportClient, the RestClient must be closed when it is not needed anymore or when the application is stopped.

So the code that closes the TransportClient:

transportClient.close();

Must be replaced with:

lowLevelRestClient.close();