All Products
Search
Document Center

Elasticsearch:Low Level REST Client (5.x)

Last Updated:Mar 26, 2026

This topic describes how to use the Java Low Level REST Client 5.x to call Elasticsearch APIs from Java over HTTP.

Important

The Java Low Level REST Client 5.x is compatible only with Alibaba Cloud Elasticsearch V5.5.3. For Elasticsearch V6.3.2, see Java REST Client 6.3.2.

Prerequisites

Before you begin, make sure you have:

  • JDK 1.8 or later. See Install a JDK.

  • An Alibaba Cloud Elasticsearch V5.5.3 cluster. 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: Error

  • Network access configured between your Java server and the cluster:

    • Internet access: Enable the Public Network Access feature and add your server's public IP address to the cluster's public IP address whitelist. See Configure a public or private IP address whitelist for an Elasticsearch cluster.

      Important
      • If your client is on a home network or a corporate LAN, add the Internet egress IP address to the whitelist, not the client's private IP address.

      • Adding 0.0.0.0/0 to the whitelist allows all public IPv4 addresses to reach the cluster. Evaluate the security risk before using this setting.

      • A missing or incorrect whitelist entry causes a "Timeout connecting" error.

      • To access Kibana from a client, configure a separate whitelist for Kibana. See Configure a public or private IP address whitelist for Kibana.

    • VPC access: Use the cluster's internal endpoint. By default, 0.0.0.0/0 is already in the private IP address whitelist.

  • A Java Maven project.

Add Maven dependencies

Add the following dependencies to your pom.xml. The client version must match your Elasticsearch cluster version.

<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>
Important

A remote code execution (RCE) vulnerability may exist in Apache Log4j 2. See Vulnerability notice | RCE vulnerability in Apache Log4j 2.

Connect to the cluster and run requests

You can download the complete sample code.

All examples connect to the cluster over port 9200 and use BasicCredentialsProvider for authentication.

Initialize the client

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;

// Configure credentials
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
        new UsernamePasswordCredentials("<username>", "<password>"));

// Build the client. Replace <host> with the public or internal endpoint
// from the Basic Information page of your cluster.
RestClient restClient = RestClient.builder(new HttpHost("<host>", 9200))
        .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(
                    HttpAsyncClientBuilder httpClientBuilder) {
                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            }
        }).build();

Index and retrieve a document

The following example indexes a document and then retrieves it:

import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import java.io.IOException;
import java.util.Collections;

public class RestClientTest55 {
    public static void main(String[] args) {

        // (Initialize restClient as shown above)

        try {
            // Index a document with PUT /index_name/type_name/doc_id
            HttpEntity entity = new NStringEntity(
                    "{\n\"field_01\" : \"value_01\"\n,\n\"field_02\" : \"value_02\"\n}",
                    ContentType.APPLICATION_JSON);
            Response indexResponse = restClient.performRequest(
                    "PUT",
                    "/index_name/type_name/doc_id",
                    Collections.<String, String>emptyMap(),
                    entity);

            // Retrieve the document with GET /index_name/type_name/doc_id
            Response response = restClient.performRequest(
                    "GET",
                    "/index_name/type_name/doc_id",
                    Collections.singletonMap("pretty", "true"));

            // Print the response body
            System.out.println(EntityUtils.toString(response.getEntity()));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Replace the following placeholders with your actual values:

Placeholder Description
<username> Username of the Elasticsearch cluster
<password> Password of the Elasticsearch cluster
<host> Public or internal endpoint of the cluster. Find this on the Basic Information page. See View the basic information of a cluster.
index_name Name of the index
type_name Name of the type
doc_id Document ID

Troubleshooting

Symptom Likely cause Fix
Timeout connecting IP address whitelist missing or incorrect Add your server's IP address to the cluster's whitelist. For Internet access, use the Internet egress IP, not the client's private IP.
Index creation error Error Auto Indexing is disabled Enable the Auto Indexing feature. See Configure the YML file.

What's next