NOTE: You are looking at documentation for an older release. For the latest information, see the current release documentation.
Changing the client’s initialization code
editChanging the client’s initialization code
editThe 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("localhost"), 9300))
.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9301));
The initialization of a RestHighLevelClient is different. It requires to provide
a low-level client builder as a constructor
argument:
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
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.
The RestHighLevelClient is thread-safe. It is typically instantiated by the
application at startup time or when the first request is executed.
Once the RestHighLevelClient is initialized, it can be used to execute any
of the supported APIs.
As with the TransportClient, the RestHighLevelClient must be closed when it
is not needed anymore or when the application is stopped.
The code that closes the TransportClient:
transportClient.close();
must be replaced with:
client.close();