This topic describes how to run curl commands to access an Alibaba Cloud Elasticsearch cluster, check cluster information, create indexes and documents, and search for documents.

Prerequisites

An Elastic Compute Service (ECS) instance is created in the same region, zone, and Virtual Private Cloud (VPC) as your Elasticsearch cluster. For more information, see Create an instance by using the provided wizard.
Notice You can also use an existing ECS instance. Make sure that the ECS instance is deployed in the same region, zone, and VPC as your Elasticsearch cluster. For more information about how to use an ECS instance in a classic network to access an Elasticsearch cluster in a VPC, see Connect to Alibaba Cloud Elasticsearch from a classic network.

Background information

The open-source Elasticsearch provides a series of RESTful APIs that you can use by running curl commands or in the Kibana console. You can refer to this topic to call RESTful APIs by running curl commands. For more information about how to use RESTful APIs in the Kibana console, see Quick start.
Notice In the sample code across this topic, replace es_password with the password of your Elasticsearch cluster.

Run curl commands to access your Elasticsearch cluster

  1. Connect to the ECS instance.
    For more information, see Step 3: Connect to the ECS instance.
  2. Run the following curl command to access your Elasticsearch cluster:
    Note If the system displays curl command not found, you must run the yum install curl command to install cURL in your ECS instance.
    curl -u <username>:<password> http://<host>:<port>
    Variable Description
    <username> The account for accessing the Elasticsearch cluster. We recommend that you use an account other than elastic.
    Notice
    • If you change the password of the elastic account, the new password may take a period of time to take effect. During this period of time, you cannot use the elastic account to access the Elasticsearch cluster. Therefore, we recommend that you do not use the elastic account.
    • 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 for accessing the Elasticsearch cluster. Enter the password specified when you create the Elasticsearch cluster or initialize Kibana.
    <host> The internal endpoint of the Elasticsearch cluster. You can find the internal endpoint on the Basic Information page of the Elasticsearch cluster.
    <port> The port of the Elasticsearch cluster. The default port is 9200. You can find the port number on the Basic Information page of the Elasticsearch cluster.
    The following code shows how to access an Elasticsearch cluster.
    curl -u elastic:es_password http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200
    If the command is successfully executed, the following result is returned.Successful response

Run a curl command to check cluster information

  • Check the health status of the cluster.
    curl -u elastic:es_password -XGET 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/_cat/health?v'

    If the command is successfully executed, the following result is returned.

    Check the health status of the cluster
  • Check indexes in the cluster.
    curl -u elastic:es_password -XGET 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/_cat/indices? v'

    If the command is successfully executed, the following result is returned.

    Check indexes in the cluster

Run a curl command to create an index or a document

  • Create an index.
    curl -u elastic:es_password -XPUT 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/product_info'

    The preceding example creates an index named product_info.

    If the command is successfully executed, the following result is returned.Create an index
  • Set a mapping for the index.
    curl -u elastic:es_password -XPUT 'http://es-cn-vxxxxxmedp.elasticsearch.aliyuncs.com:9200/product_info/_doc/_mapping' -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 code is successfully executed, the following result is returned.

    Set a mapping for the index

    The preceding example creates an index named product_info. The index is of the _doc type and contains the productName, annual_rate, and describe fields. The preceding example also defines the tokenizer for different types of fields.

  • Create a document and insert data.
    curl -u elastic:es_password -XPOST 'http://es-cn-vxxxxxmedp.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 code is executed successfully, the following result is returned:Create a document and insert data

    The preceding example creates a document named 1 in the product_info index and the index is of the _doc type, and inserts a data record into the document.

Run a curl command to search for a document

curl -u elastic:es_password -XGET 'http://es-cn-vxxxxxmedp.elasticsearch.aliyuncs.com:9200/product_info/_doc/1? pretty'
If the command is executed successfully, the following result is returned:Search for a document

The preceding example searches for a document named 1.

Run a curl command to delete an index

curl -u elastic:es_password -XDELETE 'http://es-cn-vxxxxxmedp.elasticsearch.aliyuncs.com:9200/product_info'
If the command is executed successfully, the following result is returned:Delete an index

The preceding example deletes an index named product_info.

Note For more information about commands, see open-source Elasticsearchl documentation.