Java High Level REST Client 6.7.x lets you call Elasticsearch APIs from Java applications. This guide walks you through setting up the client and running index and document operations against an Alibaba Cloud Elasticsearch cluster.
Prerequisites
Before you begin, ensure that you have:
-
JDK 1.8 or later. See Install a JDK
-
An Alibaba Cloud Elasticsearch cluster at V6.7.0 or later. See Create an Alibaba Cloud Elasticsearch cluster
-
Auto Indexing enabled on the cluster. See Configure the YML file. If Auto Indexing is not enabled, the following error appears:

-
A Java Maven project
Java High Level REST Client is forward compatible. For example, the 6.7.0 client works with Elasticsearch clusters at V6.7.0 or later. To use the latest client features, match the client version to your cluster version.
Network access
Configure your cluster's IP address whitelist so the client can reach it:
-
Public network: If your server is on the public internet, enable Public Network Access for the cluster and add the server's public IP address to the public IP address whitelist. See Configure a public or private IP address whitelist for an Elasticsearch cluster.
Important-
If the client is behind a home network or office LAN, add the internet egress IP address, not the client's private IP address.
-
Adding
0.0.0.0/0allows all IPv4 addresses and introduces security risks. Evaluate the risks before using this setting. -
A missing or misconfigured whitelist causes a "Timeout connecting" error.
-
To access Kibana from a client, configure a separate IP address whitelist for Kibana. See Configure a public or private IP address whitelist for Kibana.
-
-
VPC (virtual private cloud): If your server is in the same VPC as the cluster, use the cluster's private endpoint. By default,
0.0.0.0/0is already added to the private IP address whitelist.
Add POM dependencies
Add the following dependencies to the pom.xml file of your Maven project:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
</dependency>
A remote code execution (RCE) vulnerability may exist in Apache Log4j. See Vulnerability notice | RCE vulnerability in Apache Log4j 2.
Configure RequestOptions
Java REST Client 6.7.0 introduces RequestOptions based on Java REST Client 6.3.2, which lets you set per-request options without affecting request processing. In memory-constrained Java Virtual Machine (JVM) environments, use RequestOptions to cap the asynchronous response cache size:
private static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
// The default cache size is 100 MiB. Change it to 30 MiB.
builder.setHttpAsyncResponseConsumerFactory(
new HttpAsyncResponseConsumerFactory
.HeapBufferedResponseConsumerFactory(30 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}
Pass COMMON_OPTIONS when executing requests:
IndexResponse indexResponse = highClient.index(indexRequest, COMMON_OPTIONS);
For the full RequestOptions API, see the Elasticsearch RequestOptions reference.
Example
Download the complete sample code.
The example below creates a RestHighLevelClient, indexes a document, and deletes it. It uses COMMON_OPTIONS to limit the asynchronous response cache size to 30 MiB.
Replace each placeholder enclosed in {} with your actual values. See the inline comments for details.
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class RestClientTest67 {
private static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
// The default cache size is 100 MiB. Change it to 30 MiB.
builder.setHttpAsyncResponseConsumerFactory(
new HttpAsyncResponseConsumerFactory
.HeapBufferedResponseConsumerFactory(30 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}
public static void main(String[] args) {
// Configure basic access authentication using the cluster credentials.
// Use the username and password set when you created the cluster.
// The same credentials are used to log in to the Kibana console.
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("{Username}", "{Password}"));
// Build the client. Get the cluster endpoint from the Basic Information page.
RestClientBuilder builder = RestClient.builder(
new HttpHost("{Endpoint of the Elasticsearch cluster}", 9200, "http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(
HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
// Create the high-level client from the low-level client builder.
RestHighLevelClient highClient = new RestHighLevelClient(builder);
try {
// Build an index request.
// field_01 and field_02 are field names; value_01 and value_02 are their values.
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("{field_01}", "{value_01}");
jsonMap.put("{field_02}", "{value_02}");
// index_name: index name. type_name: type name. doc_id: document ID.
IndexRequest indexRequest =
new IndexRequest("{index_name}", "{type_name}", "{doc_id}").source(jsonMap);
// Index the document using the custom RequestOptions.
IndexResponse indexResponse = highClient.index(indexRequest, COMMON_OPTIONS);
long version = indexResponse.getVersion();
System.out.println("Index document successfully! " + version);
// Delete the document. Use the same index name, type name, and document ID.
DeleteRequest request =
new DeleteRequest("{index_name}", "{type_name}", "{doc_id}");
DeleteResponse deleteResponse = highClient.delete(request, COMMON_OPTIONS);
System.out.println("Delete document successfully! \n"
+ deleteResponse.toString() + "\n" + deleteResponse.status());
highClient.close();
} catch (IOException ioException) {
// Handle exceptions.
}
}
}
What's next
For the full Java High Level REST Client API reference, see the official Elasticsearch Java High Level REST Client documentation.