×
Community Blog Mastering Elasticsearch on Alibaba Cloud with Curl Commands and API Operations

Mastering Elasticsearch on Alibaba Cloud with Curl Commands and API Operations

This article provides a step-by-step guide along with code examples to help you query cluster information, create indexes and documents, and perform searches.

Introduction

Open source Elasticsearch offers powerful RESTful API operations accessible via curl commands or Kibana console. This guide explains how to use curl commands and API operations to access and manage an Alibaba Cloud Elasticsearch cluster.

Background Information

You can use curl commands for direct API calls or refer to the Kibana console documentation for further details.

Prerequisites

  • An Elastic Compute Service (ECS) instance running Linux in the same Virtual Private Cloud (VPC) as your Alibaba Cloud Elasticsearch cluster. For setup instructions, see Create an ECS instance using the wizard.

Accessing an Elasticsearch Cluster

Step 1: Connect to the ECS Instance

If you're using a common user:

ssh <your_username>@<ecs_instance_ip>

Step 2: Install Curl (if not already installed)

sudo yum install curl

Step 3: Access Your Elasticsearch Cluster

curl -u <user>:<password> http://<host>:<port>
  • <user>: Username for your Elasticsearch cluster, other than elastic.
  • <password>: Password for the Elasticsearch cluster.
  • <host>: Internal endpoint of the Elasticsearch cluster.
  • <port>: Default port number (9200).

Sample Command:

curl -u your_username:your_password http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200

Querying Cluster Information

Query Health Status

curl -u <user>:<password> -XGET 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/_cat/health?v'

Query Indexes in the Cluster

curl -u <user>:<password> -XGET 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/_cat/indices?v'

Creating Indexes and Documents

Create an Index

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

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"}
      }
  }
}'

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"
}'

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"}'

Searching and Deleting Documents

Search for a Document

curl -u <user>:<password> -XGET 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/product_info/_doc/1?pretty'

Delete an Index

curl -u <user>:<password> -XDELETE 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/product_info'

For more information about additional commands, refer to the official open source Elasticsearch documentation.

Conclusion

Managing your Alibaba Cloud Elasticsearch cluster with curl commands and API operations offers a flexible and efficient way to handle your data. Ready to start your journey with Elasticsearch on Alibaba Cloud?
Explore our tailored Cloud solutions and services to transform your data into a visual masterpiece. Click here to embark on Your 30-Day Free Trial

0 1 0
Share on

Data Geek

97 posts | 4 followers

You may also like

Comments

Data Geek

97 posts | 4 followers

Related Products

  • Alibaba Cloud Elasticsearch

    Alibaba Cloud Elasticsearch helps users easy to build AI-powered search applications seamlessly integrated with large language models, and featuring for the enterprise: robust access control, security monitoring, and automatic updates.

    Learn More
  • OpenAPI Explorer

    OpenAPI Explorer allows you to call an API through its web interface or WebCLI, and view the entire process.

    Learn More
  • API Gateway

    API Gateway provides you with high-performance and high-availability API hosting services to deploy and release your APIs on Alibaba Cloud products.

    Learn More
  • CloudBox

    Fully managed, locally deployed Alibaba Cloud infrastructure and services with consistent user experience and management APIs with Alibaba Cloud public cloud.

    Learn More