This topic describes how to use the Elasticsearch Java APIs. Java Low Level REST Client 5.x is used as an example.

Precautions

  • Low Level REST Client 5.x is compatible only with Alibaba Cloud Elasticsearch V5.5.3. If the version of your Elasticsearch cluster is V6.3.2, see Java REST Client 6.3.2.
  • The version of the Java REST Client must match that of your Elasticsearch cluster.

Preparations

  • Install a JDK. The JDK version must be 1.8 or later.

    For more information, see Install the JDK.

  • Create an Alibaba Cloud Elasticsearch cluster whose version is V5.5.3.

    For more information, see Create an Elasticsearch cluster.

  • Enable the Auto Indexing feature for the Elasticsearch cluster.

    For more information, see Enable auto indexing.

    If the Auto Indexing feature is not enabled, the following error is reported.Error
  • Enable the public endpoint of your Elasticsearch cluster and add the public IP address of your host to the public whitelist of the cluster.

    For more information, see Configure a whitelist to access an Elasticsearch cluster over the Internet or a VPC.

    Note If you want to query the public IP address of your host, open Google Chrome, enter IP in the search box, and click What Is My IP Address. You can obtain the public IP address of your host on the web page that appears.
  • Create a Java Maven project and add the Project Object Model (POM) dependencies to the pom.xml file of the Java project. For more information, see POM dependencies.

POM dependencies

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>parent</artifactId>
    <version>5.5.3</version>
</dependency>

Examples

Use Java REST Client to connect to Alibaba Cloud Elasticsearch over port 9200 for test. The sample code is as follows:

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 RestClientTest {
    public  static void main(String[]args){
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("<USER NAME>", "<PASSWORD>"));
        RestClient restClient = RestClient.builder(new HttpHost("<HOST>", 9200))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                }).build();
        try {
            //index a document
            HttpEntity entity = new NStringEntity("{\n\"user\" : \"kimchy\"\n}", ContentType.APPLICATION_JSON);
            Response indexResponse = restClient.performRequest(
                    "PUT",
                    "/index/type/123",
                    Collections. <String, String>emptyMap(),
                    entity);
            //search a document
            Response response = restClient.performRequest("GET", "/index/type/123",
                    Collections.singletonMap("pretty", "true"));
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Parameter Description
<USER NAME> Replace it with the username of the Elasticsearch cluster.
<PASSWORD> Replace it with the password of the Elasticsearch cluster.
<HOST> Replace it with the public or internal endpoint. You can query the endpoint on the Basic Information page of the Elasticsearch cluster.