Use Java High Level REST Client 6.3.x to call Elasticsearch Java APIs to index and delete documents in your Alibaba Cloud Elasticsearch cluster.
Prerequisites
Before you begin, ensure that you have:
-
JDK 1.8 or later installed. See Install a JDK.
-
An Alibaba Cloud Elasticsearch cluster at version 6.3.2 or later. Java High Level REST Client is forward compatible, so a 6.3.2 client can communicate with clusters of V6.3.2 or later. To use all features of the client, keep the client version the same as your cluster version. See Create an Alibaba Cloud Elasticsearch cluster.
-
Auto Indexing enabled for the cluster. See Configure the YML file. If Auto Indexing is not enabled, the following error is reported.

-
Network access configured for the cluster:
-
Internet access: Enable the Public Network Access feature and add the server's public IP address to the public IP address whitelist. If your client is in a home network or office LAN, add the Internet egress IP address—not the client's private IP address—to the whitelist. Adding
0.0.0.0/0allows all IPv4 addresses, which poses security risks. See Configure a public or private IP address whitelist for an Elasticsearch cluster. -
VPC access: Use the internal endpoint. The private IP address whitelist defaults to
0.0.0.0/0, so no additional configuration is required in most cases. The server must be in the same virtual private cloud (VPC) as the cluster.
Note If no IP address whitelist is configured or the configuration is incorrect, a "Timeout connecting" error is returned. To access the Kibana node from a client, configure a separate IP address whitelist for Kibana. See Configure a public or private IP address whitelist for Kibana. -
-
A Java Maven project with the POM dependencies from the next section added to your
pom.xml.
Add POM dependencies
Add the following dependencies to the pom.xml file of your Maven project:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.3.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.3.2</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.
Connect to the cluster
Use basic access authentication and the cluster endpoint to initialize a RestHighLevelClient instance.
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.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
// Set up basic access authentication using the cluster username and password.
// These are the same credentials used to log in to the Kibana console.
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("<username>", "<password>"));
// Build the client using the cluster endpoint.
// Obtain the endpoint from the Basic Information page of the cluster.
RestClientBuilder builder = RestClient.builder(
new HttpHost("<cluster-endpoint>", 9200, "http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(
HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
RestHighLevelClient client = new RestHighLevelClient(builder);
Replace the following placeholders with the actual values:
| Placeholder | Description | Where to find it |
|---|---|---|
<username> |
Cluster username | Set when creating the cluster |
<password> |
Cluster password | Set when creating the cluster |
<cluster-endpoint> |
Public or internal endpoint of the cluster | Basic Information page of the cluster |
Index a document
Use the index API to add a document to an index.
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import java.util.HashMap;
import java.util.Map;
// Build the document content as a map.
// Replace field names and values with your own.
Map<String, Object> document = new HashMap<>();
document.put("<field_01>", "<value_01>");
document.put("<field_02>", "<value_02>");
// Specify the index name, type name, and document ID.
IndexRequest indexRequest = new IndexRequest("<index_name>", "<type_name>", "<doc_id>")
.source(document);
IndexResponse indexResponse = client.index(indexRequest);
System.out.println("Indexed document, version: " + indexResponse.getVersion());
Delete a document
Use the delete API to remove a document by its index name, type name, and document ID.
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
// Use the same index name, type name, and document ID as when you indexed the document.
DeleteRequest deleteRequest = new DeleteRequest("<index_name>", "<type_name>", "<doc_id>");
DeleteResponse deleteResponse = client.delete(deleteRequest);
System.out.println("Deleted document successfully.");
Complete example
The following example combines client initialization, document indexing, and document deletion into a single runnable program. You can also download the complete sample code.
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.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class RestClientTest63 {
public static void main(String[] args) {
// Set up basic access authentication.
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("{Username}", "{Password}"));
// Initialize the client using the cluster endpoint.
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);
}
});
RestHighLevelClient highClient = new RestHighLevelClient(builder);
try {
// Index a document.
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("{field_01}", "{value_01}");
jsonMap.put("{field_02}", "{value_02}");
IndexRequest indexRequest = new IndexRequest("{index_name}", "{type_name}", "{doc_id}")
.source(jsonMap);
IndexResponse indexResponse = highClient.index(indexRequest);
System.out.println("Indexed document, version: " + indexResponse.getVersion());
// Delete the document using the same index name, type name, and document ID.
DeleteRequest deleteRequest = new DeleteRequest("{index_name}", "{type_name}", "{doc_id}");
DeleteResponse deleteResponse = highClient.delete(deleteRequest);
System.out.println("Deleted document successfully.");
highClient.close();
} catch (IOException ioException) {
// Handle exceptions.
}
}
}
What's next
For the full list of supported APIs, see the Java High Level REST Client 6.3 documentation.