This topic describes how to use the Elasticsearch Java API. Java high-level REST client 6.3.x is used as an example.


  • The JDK is installed. The JDK version must be 1.8 or later.
  • An Alibaba Cloud Elasticsearch instance is created. The instance version must be later than or equal to the version of the high-level client. In this topic, an instance of version 6.3.2 is created.
    Notice The high-level client is forward compatible. For example, a high-level client of version 6.3.2 can communicate with Elasticsearch clusters of version 6.3.2 and later versions. To ensure that you can use the features of the latest client, we recommend that you use the same high-level client version as the cluster version.
  • You have created a Java Maven project and added the Pom dependency to the pom.xml file of the Java project.Add the pom dependency
    Note This topic uses Elasticsearch 6.3.2 as an example.

Pom dependency



The following sample code uses the index API to index the document, and then uses the delete API to delete the document.

// The file path is src/main/java/
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.util.HashMap;
import java.util.Map;

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

        // The Alibaba Cloud Elasticsearch cluster requires basic authentication.
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
       // Use the username and password that you have specified when creating the Alibaba Cloud Elasticsearch instance. The username and password are also used to log on to the Kibana console.
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("{username}", "{password}"));

       // Create a REST client by using the builder and configure HttpClientConfigCallback for the HTTP client.
       // Click the ID of the Elasticsearch instance. On the Basic Information page, obtain the public IP address of the Elasticsearch cluster.
        RestClientBuilder builder = RestClient.builder(new HttpHost("{Elasticsearch cluster address}", 9200))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

        // Create a RestHighLevelClient instance by using the REST low-level client builder.
        RestHighLevelClient highClient = new RestHighLevelClient(builder);

        try {
            // Create a request.
            Map<String, Object> jsonMap = new HashMap<>();
           // field_01 and field_02 are the field names, and value_01 and value_02 are the corresponding values.
            jsonMap.put("{field_01}", "{value_01}");
            jsonMap.put("{field_02}", "{value_02}");
            // index_name is the index name, type_name is the type name, and doc_id is the document ID.       
            IndexRequest indexRequest = new IndexRequest("{index_name}", "{type_name}", "{doc_id}").source(jsonMap);

            // Run the following command in parallel.
            IndexResponse indexResponse = highClient.index(indexRequest);

            long version = indexResponse.getVersion();

            System.out.println("Index document successfully! " + version);
            // 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 that you have specified when creating the index.
            DeleteRequest request = new DeleteRequest("{index_name}", "{type_name}", "{doc_id}");
            DeleteResponse deleteResponse = highClient.delete(request);

            System.out.println("Delete document successfully!") ;


        } catch (IOException ioException) {
            // Handle exceptions.

You can replace the parameters in the preceding sample code that are contained within braces {} with service-specific parameters. For more information, see the code comments.