This topic describes how to use curl commands to access an Alibaba Cloud Elasticsearch cluster, query cluster information, create indexes and documents, and search for documents.
Prerequisites
Background information
es_password
with the password of your Elasticsearch cluster.
Run a curl command to access your Elasticsearch cluster
Run a curl command to query cluster information
- Query 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 result shown in the following figure is returned.
- Query 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 result shown in the following figure is returned.
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'
In the preceding example, an index named
product_info
is created.If the command is successfully executed, the result shown in the following figure is returned. - Set a mapping for an index
curl -u elastic:es_password -XPUT 'http://es-cn-vxxxxx****.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 command is successfully executed, 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 theproductName
,annual_rate
, anddescribe
fields. This example also defines a tokenizer for the fields. - Create a document and insert data
curl -u elastic:es_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 executed, the result shown in the following figure is returned.In the preceding example, a document named
1
is created in theproduct_info
index of the_doc
type, and a data record is inserted into the document.
Run a curl command to search for a document
curl -u elastic:es_password -XGET 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/product_info/_doc/1?pretty'

In the preceding example, a document named 1
is searched for.
Run a curl command to delete an index
curl -u elastic:es_password -XDELETE 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/product_info'

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