This topic describes how to use developer tools, such as Alibaba Cloud CLI, OpenAPI Explorer, and Alibaba Cloud SDKs, to call an Elasticsearch API operation. This topic uses the ListSearchLog operation as an example.

Background information

Before you call an API operation, you must understand related instructions in API documentation and obtain the values of the required request parameters. If an error is reported after you send a request, you can obtain the description of the error code in API documentation.

Methods to make API requests

  • Alibaba Cloud CLI
    If you want to use a command line tool, you can use this method. Before you use this method, make sure that Alibaba Cloud CLI is installed on the host that you use to send requests. For more information about how to install Alibaba Cloud CLI in different operating systems, see the following topics:
  • OpenAPI Explorer

    You can use this method if you want to use an interactive operation interface or you are a developer user who is unfamiliar with Alibaba Cloud products. You can also debug API operations and obtain sample SDK requests in OpenAPI Explorer. For more information, see What is OpenAPI Explorer?

  • SDK for Java

    SDK for Java is ideal for scenarios such as SDK encoding or DevOps. Before you use SDK for Java, you must install a JDK and Maven. The JDK version must be 1.6 or later.

Alibaba Cloud CLI

  1. Use one of the following methods to obtain the ID of the cluster you want to access:
    • In the Kibana console, run the GET / command. For more information, see Log on to the Kibana console.
    • On your on-premises machine, call the ListInstance operation.
      aliyun elasticsearch ListInstance --zoneId cn-hangzhou
  2. Call the ListSearchLog operation to query the system logs of the cluster.
    aliyun elasticsearch ListSearchLog --type INSTANCELOG --query level:warn --beginTime 1593332477000 --endTime 1593418877000 --page 1 --size 20 --InstanceId es-cn-n6w1o1x0w00******   

OpenAPI Explorer

  1. Call the ListInstance operation to obtain the ID of the cluster you want to access.
  2. Call the ListSearchLog operation to query the system logs of the cluster.
    If the query is successful, OpenAPI Explorer displays related logs.

SDK for Java

  1. Create a Maven project.
    Note For more information, see Install ECS SDK for Java.
  2. Add POM dependencies.
     <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-elasticsearch</artifactId>
            <version>3.0.16</version>
      </dependency>
      <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.6</version>
       </dependency>
  3. Create a Java program. Then, call the ListSearchLog operation to query the system logs of the cluster that you want to access.
    import com.aliyuncs.CommonRequest;
    import com.aliyuncs.CommonResponse;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.MethodType;
    import com.aliyuncs.profile.DefaultProfile;
    
    public class ListSearchLog {
        public static void main(String[] args) {
            DefaultProfile profile = DefaultProfile.getProfile("<RegionId>", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            CommonRequest request = new CommonRequest();
            //request.setProtocol(ProtocolType.HTTPS);
            request.setMethod(MethodType.GET);
            request.setDomain("elasticsearch. <RegionId>.aliyuncs.com");
            request.setVersion("2017-06-13");
            request.setUriPattern("/openapi/instances/<instanceid>/search-log");
            request.putQueryParameter("type", "INSTANCELOG");
            request.putQueryParameter("query", "level:warn");
            request.putQueryParameter("beginTime", "1593332477000");
            request.putQueryParameter("endTime", "1593418877000");
            request.putQueryParameter("page", "1");
            request.putQueryParameter("size", "20");
            request.putHeadParameter("Content-Type", "application/json");
            String requestBody = "" +
                    "{}";
            request.setHttpContent(requestBody.getBytes(), "utf-8", FormatType.JSON);
            try {
                CommonResponse response = client.getCommonResponse(request);
                System.out.println(response.getData());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }
    Parameter Description
    <RegionId> The ID of the region where the cluster resides. For more information about how to obtain the ID, see Types of resources that can be authorized.
    <accessKeyId> The AccessKey ID of your Alibaba Cloud account. For more information about how to obtain the AccessKey ID, see Create an AccessKey pair.
    <accessSecret> The AccessKey secret of your Alibaba Cloud account. For more information about how to obtain the AccessKey secret, see Create an AccessKey pair.
    <instanceId> The ID of the cluster. For more information about how to obtain the ID, see View the basic information of a cluster and ListInstance.
    Note For more information about request parameters, see ListSearchLog.