Java REST Client accessing Alibaba cloud 5.5 elastic search instance implementation

Development environment: InteliJ IDEA

Operating system: Mac OS Mojave

Elastic search version: Alibaba cloud 5.5.3 with x-pack

Client version: REST Client 5.5.3

1. Create Alibaba cloud ES instance in advance and open the public address access white list.

2. Pre create index and mapping (using Kibana Dev Tools)

PUT index_test
  "mappings": {
    "book": {
      "properties" : {
        "book_id" : {
        "name" : {

3. Create project and RestClient class

4. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""


        <!-- -->


The Java REST Client Demo here is mainly applicable to Alibaba cloud es version 5.5.3, which is not compatible with Alibaba cloud es version 6.3.2.

Because the Alibaba cloud elastic search instance uses version 5.5.3, you need to have at least version 1.8 of your JDK.

The Java REST Client version needs to be consistent with the ES instance version.

5. Build REST Client object for access

Step one:

Because Alibaba cloud es requires the elasticsearch HTTP basic authentication, the official guidance of ES is: when the RestClient object is constructed through builder, the callback interface HttpClientConfigCallback of builder is set. The callback interface has only one implementation method, customizeHttpClient(), which takes a httapasyncclientbuilder object, sets the validation information of the object (through CredentialProvider), and returns the object.

This method is used to set the authentication information for RestClient, which will be brought with the subsequent request to ES server.

Step two:

The JSON request is spliced through HttpEntity, and the request is initiated through restClient.performRequest(). The main demonstration of this example is to create an index document and retrieve it.

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.util.Collections;

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

        // Step 1: create a RestClient object
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                new UsernamePasswordCredentials("ES Instance user name", "ES Instance password"));
        RestClient restClient = RestClient.builder(new HttpHost("ES Instance public network address", 9200))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

        // Step 2: initiate the request
        try {

            //index a document add a piece of data to ES index
            HttpEntity entity = new NStringEntity("{\n\"book_id\":\"0001\",\n\"name\":\"Alice in Wonderland\"\n}",
            Response indexResponse = restClient.performRequest(
                    Collections.<String, String>emptyMap(),

            //search a document to retrieve ES data
            Response response = restClient.performRequest("GET", "/index_test/book/0001",
                    Collections.singletonMap("pretty", "true"));
        } catch (IOException e) {

The document was successfully created and retrieved. The request was successful!

Keywords: Java Apache REST ElasticSearch Maven

Added by shanejeffery86 on Mon, 09 Dec 2019 17:39:27 +0200