×
Community Blog Guide to Managing Alibaba Cloud Elasticsearch Clusters with API Operations

Guide to Managing Alibaba Cloud Elasticsearch Clusters with API Operations

You’ll learn to query cluster information, create indexes and documents, and execute document searches with ease.

Introduction

Discover how to effortlessly manage your Alibaba Cloud Elasticsearch cluster through API operations. This comprehensive guide provides essential insights into using curl commands and navigating API operations for accessing an Alibaba Cloud Elasticsearch cluster. You’ll learn to query cluster information, create indexes and documents, and execute document searches with ease.

Alibaba Cloud Elasticsearch: Please Click here, Embark on Your 30-Day Free Trial !!

Understanding the Essentials

The ability to run API operations through curl commands is a cornerstone for managing Alibaba Cloud Elasticsearch clusters. Whether you’re a beginner or an experienced user, the steps outlined here will guide you through seamless cluster access and management. For those looking to use the Kibana console for API operations, our dedicated section offers the detailed information you need.

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

NoteIn 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>
Variable Description
<user> The username that is used to access your Elasticsearch cluster. We recommend that you use a username other than elastic.Important
<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.

1)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

2)If the version of your Elasticsearch cluster contains with_X-Pack, you must specify both the username and password to access the Elasticsearch cluster.

Query the Information of the Cluster

1)Query the health status of the cluster

2)Query indexes in 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.

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.

Create Indexes and Documents

1)Create an index

2)Configure a mapping for an index

3)Create documents and insert data

  1. Create a single document
  2. Create multiple documents
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.

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.

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.

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.

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.

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.

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.

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.

In the preceding example, an index named product_info is deleted.
Note
For information about other commands, see open source Elasticsearch documentation

Start Your Journey with Elasticsearch on Alibaba Cloud for Free

Ready to start your journey with elasticsearch on Alibaba Cloud? Explore our tailored Cloud solutions and services to take the first step towards transforming your data into a visual masterpiece.Please Click here, Embark on Your 30-Day Free Trial !!

Learn more about New Features of Alibaba Cloud Elasticsearch

0 1 0
Share on

Data Geek

36 posts | 2 followers

You may also like

Comments