The Elasticsearch Java API Client is an entirely new client library that has no relation to the older High Level Rest Client (HLRC). This was a deliberate choice to provide a library that is independent from the Elasticsearch server code and that provides a very consistent and easier to use API for all Elasticsearch features.
Migrating from the HLRC therefore requires some code rewrite in your application. This transition can however happen progressively as the two client libraries can coexist in a single application with no operational overhead.
Compatibility mode: using a 7.17 client with Elasticsearch 8.xedit
The HLRC version
7.17 can be used with Elasticsearch version
8.x by enabling
HLRC’s compatibility mode (see code sample below). In this mode HLRC sends
additional headers that instruct Elasticsearch
8.x to behave like a
The Java API Client doesn’t need this setting as compatibility mode is always enabled.
Using the same http client with the HLRC and the Java API Clientedit
To avoid any operational overhead during the transition phase where an application would use both the HLRC and the new Java API Client, both clients can share the same Low Level Rest Client, which is the network layer that manages all connections, round-robin strategies, node sniffing, and so on.
The code below shows how to initialize both clients with the same HTTP client:
// Create the low-level client RestClient httpClient = RestClient.builder( new HttpHost("localhost", 9200) ).build(); // Create the HLRC RestHighLevelClient hlrc = new RestHighLevelClientBuilder(httpClient) .setApiCompatibilityMode(true) .build(); // Create the Java API Client with the same low level client ElasticsearchTransport transport = new RestClientTransport( httpClient, new JacksonJsonpMapper() ); ElasticsearchClient esClient = new ElasticsearchClient(transport); // hlrc and esClient share the same httpClient
There are different ways you can start transitioning away from the HLRC in your application code.
- keep the existing code as-is and use the new Java API Client for new features in your application, then later migrate the existing code,
- rewrite the parts of your application where the new Java API Client is much easier to use than that of the HLRC, like everything related to search,
- rewrite those parts where you need to map application objects to/from JSON, by leveraging the tight integration of the new Java API Client with JSON object mappers.