This topic describes how to use Java Low Level REST Client 5.x to call Elasticsearch Java APIs.
Precautions
Low Level REST Client 5.x is compatible only with Alibaba Cloud Elasticsearch V5.5.3. For more information about how to use a Java REST client to access an Elasticsearch V6.3.2 cluster, see Java REST Client 6.3.2.
The version of the Java REST client must be the same as that of your Elasticsearch cluster.
Preparations
Install a JDK. The JDK version must be 1.8 or later.
For more information, see Install a JDK.
Create an Alibaba Cloud Elasticsearch V5.5.3 cluster.
For more information, see Create an Alibaba Cloud Elasticsearch cluster.
Enable the Auto Indexing feature for the Elasticsearch cluster.
For more information, see Configure the YML file.
If the Auto Indexing feature is not enabled, the following error is reported.
Configure an IP address whitelist for the Elasticsearch cluster to ensure normal communication among networks.
If the server that runs Java code is located in an Internet environment, you can access the cluster by using its public endpoint. Before you access the cluster, you must enable the Public Network Access feature for the cluster and add the public IP address of the server to a public IP address whitelist of the cluster. For more information, see Configure a public or private IP address whitelist for an Elasticsearch cluster.
ImportantIf your client is in a home network or in a LAN of an office, you must add the IP address of the Internet egress to the whitelist rather than the private IP address of the client.
You can also add 0.0.0.0/0 to the whitelist to allow requests from all IPv4 addresses. If you make this configuration, all public IP addresses can be used to access the cluster. This poses security risks. We recommend that you evaluate the risks before you make this configuration.
If no IP address whitelist is configured or the IP address whitelist is incorrectly configured, the system reports the "Timeout connecting" error message to indicate a connection timeout error.
If you want to access the Kibana node in your cluster from a client, you must configure an IP address whitelist for Kibana. For more information, see Configure a public or private IP address whitelist for Kibana.
If the server that runs Java code is located in the same virtual private cloud (VPC) as the Elasticsearch cluster, you can access the cluster by using its internal endpoint. Before you access the cluster, make sure that the private IP address of the server is added to a private IP address whitelist of the cluster. By default, 0.0.0.0/0 is added to the whitelist.
Create a Java Maven project and add the following Project Object Model (POM) dependencies to the pom.xml file of the project.
POM dependencies
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>rest</artifactId>
<version>5.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7.1</version>
</dependency>
A remote code execution (RCE) vulnerability may exist in Apache Log4j. For more information, see Vulnerability notice | RCE vulnerability in Apache Log4j 2.
Example
You can download the complete sample code.
In the following sample code, the Java REST client is connected to the Elasticsearch cluster over port 9200.
import org.apache.http.HttpEntity;
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.entity.ContentType;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import java.io.IOException;
import java.util.Collections;
public class RestClientTest55 {
public static void main(String[]args){
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("USER NAME", "PASSWORD"));
// Set HOST to the public endpoint of the Elasticsearch cluster. You can obtain the endpoint from the Basic Information page of the cluster.
RestClient restClient = RestClient.builder(new HttpHost("HOST", 9200))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}).build();
try {
// field_01 and field_02 are field names, and value_01 and value_02 are the values of field_01 and field_02.
HttpEntity entity = new NStringEntity("{\n\"field_01\" : \"value_01\"\n,\n\"field_02\" : \"value_02\"\n}", ContentType.APPLICATION_JSON);
// index_name is the index name, type_name is the type name, and doc_id is the document ID.
Response indexResponse = restClient.performRequest(
"PUT",
"/index_name/type_name/doc_id",
Collections.<String, String>emptyMap(),
entity);
// index_name is the index name, type_name is the type name, and doc_id is the document ID. The index name, type name, and document ID are the same as those you specified when you create the index.
Response response = restClient.performRequest("GET", "/index_name/type_name/doc_id",
Collections.singletonMap("pretty", "true"));
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Parameter | Description |
| Replace it with the username of the Elasticsearch cluster. |
| Replace it with the password of the Elasticsearch cluster. |
| Replace it with the public or internal endpoint of the Elasticsearch cluster. You can obtain the endpoint from the Basic Information page of the cluster. For more information, see View the basic information of a cluster. |