Accessing an Elasticsearch Cluster Through the Low Level REST Client

The high-level client is encapsulated based on the low-level client. If the method calls (such as .search and .bulk) in the high-level client cannot meet the requirements or has compatibility issues, you can use the low-level client. You can even use HighLevelClient.getLowLevelClient() to directly obtain the low-level client. A low-level client allows you to customize the request structure, which is more flexible and supports all the request formats of Elasticsearch, such as GET, POST, DELETE, and HEAD.

This section describes how to use the Low Level REST Client to access the CSS cluster. The methods are as follows. For each method, you can directly create a REST low-level client, or create a high-level client and then invoke getLowLevelClient() to obtain a low-level client.

Precautions

The Low Level REST Client version should match the Elasticsearch version. For example, use Low Level REST Client 7.6.2 to access the Elasticsearch 7.6.2 cluster.

Prerequisites

Connecting to a Non-Security Cluster Through the Low Level REST Client

host indicates the IP address of the cluster. If there are multiple IP addresses, separate them using commas (,). test indicates the index name to be queried.

Connecting to a Security-Mode Cluster Through Low Level REST Client (Without a Security Certificate)

Table 1 Variables

Parameter

Description

host

IP address for accessing the Elasticsearch cluster. If there are multiple IP addresses, separate them with commas (,).

port

Access port of the Elasticsearch cluster. The default value is 9200.

protocol

Connection protocol, which can be http or https.

connectTimeout

Socket connection timeout (in ms).

connectionRequestTimeout

Socket connection request timeout (in ms).

socketTimeout

Socket request timeout (in ms).

username

Username for accessing the cluster.

password

Password of the user.

Connecting to a Security-Mode Cluster Through Low Level REST Client (With a Security Certificate)

For how to obtain and upload a security certificate, see Obtaining and Uploading a Security Certificate.

Table 2 Function parameters

Parameter

Description

host

IP address for accessing the Elasticsearch cluster. If there are multiple IP addresses, separate them with commas (,).

port

Access port of the Elasticsearch cluster. The default value is 9200.

protocol

Connection protocol. Set this parameter to https.

connectTimeout

Socket connection timeout (in ms).

connectionRequestTimeout

Socket connection request timeout (in ms).

socketTimeout

Socket request timeout (in ms).

username

Username for accessing the cluster.

password

Password of the user.

certFilePath

Certificate path.

certPassword

Certificate password.

Obtaining and Uploading a Security Certificate

To access a security-mode Elasticsearch cluster that uses HTTPS, a security certificate must be loaded. Perform the following steps to obtain the security certificate and upload it to the client:

  1. Obtain the security certificate CloudSearchService.cer.
    1. Log in to the CSS management console.
    2. In the navigation pane on the left, choose Clusters > Elasticsearch.
    3. In the cluster list, click the name of the target cluster. The cluster information page is displayed.
    4. Click the Overview tab. In the Configuration area, click Download Certificate next to HTTPS Access.
      Figure 1 Downloading a security certificate
  2. Convert the security certificate CloudSearchService.cer. Upload the downloaded security certificate to the client and use keytool to convert the .cer certificate into a .jks certificate that can be read by Java.
    • In Linux, run the following command to convert the certificate:
      keytool -import -alias newname -keystore ./truststore.jks -file ./CloudSearchService.cer 
    • In Windows, run the following command to convert the certificate:
      keytool -import -alias newname -keystore .\truststore.jks -file .\CloudSearchService.cer

    In the preceding command, newname indicates the user-defined certificate name.

    After this command is executed, you will be prompted to set the certificate password and confirm the password. Securely store the password. It will be used for accessing the cluster.