Open source Elasticsearch provides a series of RESTful API operations that you can call by running curl commands or by using the Kibana console. This topic describes how to use curl commands and call API operations to access an Alibaba Cloud Elasticsearch cluster, query cluster information, create indexes and documents, and search for documents.

Background information

You can refer to this topic to call API operations by running curl commands. For more information about how to call the API operations by using the Kibana console, see Log on to the Kibana console.

Prerequisites

An Elastic Compute Service (ECS) instance that runs the Linux operating system is created in the virtual private cloud (VPC) where your Alibaba Cloud Elasticsearch cluster resides. For more information, see Create an instance by using the wizard.
Important You can also use an existing ECS instance that resides in the same VPC as your Elasticsearch cluster.

Access an Elasticsearch cluster

  1. Connect to the ECS instance.
    For more information, see Step 3: Connect to the ECS instance.
    Note In this example, a common user is used.
  2. Run the following command to access your Elasticsearch cluster.
    Note If the system displays curl command not found, you must run the sudo yum install curl command to install cURL on the ECS instance.
    curl -u <user>:<password> http://<host>:<port>
    VariableDescription
    <user>The username that is used to access your Elasticsearch cluster. We recommend that you use a username other than elastic.
    Important
    • If you use the elastic account to access your Elasticsearch cluster and then reset the password of the account, it may require some time for the new password to take effect. During this period, you cannot use the elastic account to access the cluster. Therefore, we recommend that you do not use the elastic account to access an Elasticsearch cluster. You can log on to the Kibana console, create a user with the required role, and access the Elasticsearch cluster as the user. For more information, see Use the RBAC mechanism provided by Elasticsearch X-Pack to implement access control.
    • If the version of your Elasticsearch cluster contains with_X-Pack, you must specify both the username and password to access the Elasticsearch cluster.
    <password>The password that is used to access your Elasticsearch cluster. The password of the elastic account is specified when you create the Elasticsearch cluster. If you forget the password, you can reset it. For more information about the procedure and precautions for resetting the password, see Reset the access password for an Elasticsearch cluster.
    <host>The internal endpoint of your Elasticsearch cluster. You can obtain the internal endpoint from the Basic Information page of the Elasticsearch cluster.
    <port>The port number that is used to access your Elasticsearch cluster. The default port number is 9200. You can obtain the port number from the Basic Information page of the Elasticsearch cluster.
    Sample command:
    curl -u <user>:<password> http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200
    If the command is successfully run, the result shown in the following figure is returned.Successful response

Query the information of the cluster

  • Query the health status of the cluster
    curl -u <user>:<password> -XGET 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/_cat/health?v'
    If the command is successfully run, the result shown in the following figure is returned.Query the health status of the cluster
  • Query indexes in the cluster
    curl -u <user>:<password> -XGET 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/_cat/indices?v'
    If the command is successfully run, the result shown in the following figure is returned.Query indexes in the cluster

Create indexes and documents

  • Create an index
    curl -u <user>:<password> -XPUT 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/product_info'

    In the preceding example, an index named product_info is created.

    If the command is successfully run, the result shown in the following figure is returned.Create an index
  • Configure a mapping for an index
    curl -u <user>:<password> -XPUT 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/product_info/_doc/_mapping?include_type_name=true' -H 'Content-Type: application/json' -d '
    {
     "_doc":{
       "properties": {
            "productName": {"type": "text","analyzer": "ik_smart"},
            "annual_rate":{"type":"keyword"},
            "describe": {"type": "text","analyzer": "ik_smart"}
          }
      }
    }'
    If the command is successfully run, the result shown in the following figure is returned.Configure a mapping for an index

    In the preceding example, the type of the product_info index is set to _doc. The index contains the productName, annual_rate, and describe fields. If you use an Elasticsearch cluster of V7.0 or later, the type of indexes in the cluster must be _doc. This example also defines tokenizers for the fields.

  • Create documents and insert data
    • Create a single document
      curl -u <user>:<password> -XPOST 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/product_info/_doc/1?pretty' -H 'Content-Type: application/json' -d '
      {
      "productName":"testpro",
      "annual_rate":"3.22%",
      "describe":"testpro"
      }'
      If the command is successfully run, the result shown in the following figure is returned.Create documents and insert data

      In the preceding example, a document named 1 is created in the product_info index of the _doc type, and a data record is inserted into the document.

    • Create multiple documents
      curl -u <user>:<password> -XPOST http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/_bulk -H 'Content-Type: application/json' -d'
      { "index" : { "_index": "product_info", "_type" : "_doc", "_id" : "1" } }
      {"productName":"testpro","annual_rate":"3.22%","describe":"testpro"}
      { "index" : { "_index": "product_info", "_type" : "_doc", "_id" : "2" } }
      {"productName":"testpro1","annual_rate":"3.26%","describe":"testpro"}'
      If the command is successfully run, the result shown in the following figure is returned.Create multiple documents

      In the preceding example, documents named 1 and 2 are created in the product_info index of the _doc type, and a data record is inserted into each of the documents.

Search for a document

curl -u <user>:<password> -XGET 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/product_info/_doc/1?pretty'
If the command is successfully run, the result shown in the following figure is returned.Search for a document

In the preceding example, a document named 1 is searched for.

Delete an index

curl -u <user>:<password> -XDELETE 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/product_info'
If the command is successfully run, the result shown in the following figure is returned.Delete an index

In the preceding example, an index named product_info is deleted.

Note For information about other commands, see open source Elasticsearch documentation.

FAQ

How do I install the curl tool and use a curl command to access an Elasticsearch cluster if I want to use a Windows client to connect to the cluster?