web123456

Elasticsearch RestHighLevelClient is marked as deprecated Its alternative Basic tutorials and migration solutions for Elasticsearch Java API Client

After Elasticsearch version 7.15, Elasticsearch officially puts its advanced clientRestHighLevelClientMarked as deprecated. At the same time, a brand new Java API client was launchedElasticsearch Java API Client, this client will also become the officially recommended client in Elasticsearch 8.0 and later.

Elasticsearch Java API ClientSupports all Elasticsearch APIs except the Vector tile search API and the Find structure API. It supports all API data types and no longer has the original JsonValue attribute. It is a client for Elasticsearch 8.0 and later versions. Currently, Elasticsearch has been updated to 8.0.1, so we need to learn how to use the new Elasticsearch Java API Client.

1. Environmental Requirements

First of all, your project needs to support Java 8 or above, and your project needs to have a Json object mapping library, such as Jackson, etc. This article uses Jackson as an example.

2. Installation dependencies

Install in Gradle project:

dependencies {
    implementation ':elasticsearch-java:8.0.1'
    implementation ':jackson-databind:2.12.3'
}

Install in Maven project:

<project>

  <dependencies>
    <dependency>
      <groupId></groupId>
      <artifactId>elasticsearch-java</artifactId>
      <version>8.0.1</version>
    </dependency>
    <dependency>
      <groupId></groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.12.3</version>
    </dependency>
  </dependencies>

</project>

3. Connect

// Create a low-level client
 RestClient restClient = (
     new HttpHost("localhost", 9200)).build();

 // Create a transport layer using Jackson mapper
 ElasticsearchTransport transport = new RestClientTransport(
     restClient, new JacksonJsonpMapper());

 // Create API client
 ElasticsearchClient client = new ElasticsearchClient(transport);

4. Test query request

Note that corresponding data is required in ES to be found. If there is no data available for testing, you can skip this item directly.

SearchResponse<Product> search = (s -> s
    .index("products")
    .query(q -> q
        .term(t -> t
            .field("name")
            .value(v -> ("testname"))
        )),
    );

for (Hit<Product> hit: ().hits()) {
    processProduct(());
}

5. Basic operations of index index

5.1 Create an index

// Create a connection
 RestClient restClient = (
         new HttpHost("localhost", 9200)).build();
 ElasticsearchTransport transport = new RestClientTransport(
         restClient, new JacksonJsonpMapper());
 ElasticsearchClient client = new ElasticsearchClient(transport);

 // Create an index
 CreateIndexResponse createIndexResponse = ().create(c -> ("newapi"));
 // Print the result
 (());

 // Close the connection
 ();
 ();

Among them, newapi is the name you want to create the index.

5.2 Query index

// Create the low-level client
RestClient restClient = (
        new HttpHost("localhost", 9200)).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);
GetIndexResponse createIndexResponse = ().get(e->("newapi"));
((",", ().keySet()));
();
();

Among them, newapi is the name you want to query the index.

5.3 Delete the index

// Create the low-level client
RestClient restClient = (
        new HttpHost("localhost", 9200)).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);
DeleteIndexResponse deleteIndexResponse = ().delete(e->("newapi"));
(());
();
();

Among them, newapi is the name you want to delete the index.

6. Basic operations of document Doc

6.1 Creating a Document Doc

// Create an object that needs to be saved to ES
 Test test = new Test();
 ("Add test");
 ("male");
 (twenty four);

 // Build a request to create a Doc
 CreateResponse createResponse = (e->("newapi").id("1003").document(test));

 // Print the request result
 (());

Among them, index is the name of the index to which the document Doc belongs, and the id is the id of the document. The document parameter can now be directly passed into the Java object.

6.2 Modify the document Doc

// Build content that needs to be modified, Map is used here
 Map<String, Object> map = new HashMap<>();
         ("age", 35);

 // Request to build a modified document
 UpdateResponse<Test> response = (e -> ("newapi").id("1003").doc(map), );

 // Print the request result
 (());

6.3 Query Document Doc

// Build query request
 GetResponse<Test> response = (e -> ("newapi").id("1003"), );

 // Print query results
 (().toString());

6.4 Delete Document Doc

// Build a request to delete a document
 DeleteResponse response = (e -> ("newapi").id("1001"));

 // Print the request result
 (());

More batch and complex query-related operations of the new client Elasticsearch Java API Client have been updated. You can check my new article:Basic usage of Elasticsearch Java API Client in Elasticsearch 8.0 version_Wufeng's blog, sharing technical articles on Java and Vue directions-CSDN blog

How to migrate from the High Level Rest Client to the new client Elasticsearch Java API Client?

According to the official answer, it cannot be smoothly migrated, but the new version of the client and the old version of the advanced client can coexist and have no operational overhead. So in your project you can gradually migrate from the old client to the new client.

In addition, the official also provides a solution to make the High Level Rest Client and Elasticsearch Java API Client use the same transmission channel. It can enable two clients to share the same Low Level Rest Client, manage all connections, network layers of loop policies, and node sniffing.

The following code shows how to use the same HTTP client to initialize both the new and old clients:

// Create a low-level client (new version of client content)
 RestClientBuilder httpClientBuilder = (
     new HttpHost("localhost", 9200)
 );

 // Create a legacy high-level client RestHighLevelClient
 RestHighLevelClient hlrc = new RestHighLevelClient(httpClientBuilder);

 // Create a new Java client with the same low-level client
 ElasticsearchTransport transport = new RestClientTransport(
     (),
     new JacksonJsonpMapper()
 );

 ElasticsearchClient esClient = new ElasticsearchClient(transport);

 // Old and new clients share the same http client