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.
Prerequisites
Background information
Access your Elasticsearch cluster
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 running of the command is successful, the result shown in the following figure is returned.
- Query indexes in the cluster
curl -u <user>:<password> -XGET 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/_cat/indices?v'
If the running of the command is successful, the result shown in the following figure is returned.
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 running of the command is successful, the result shown in the following figure is returned. - Configure a mapping for an index
curl -u <user>:<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 running of the command is successful, 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 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 running of the command is successful, 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. - 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 running of the command is successful, the result shown in the following figure is returned.In the preceding example, documents named
1
and2
are created in theproduct_info
index of the_doc
type, and a data record is inserted into each of the documents.
- Create a single document
Search for a document
curl -u <user>:<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.
Delete an index
curl -u <user>:<password> -XDELETE 'http://es-cn-vxxxxx****.elasticsearch.aliyuncs.com:9200/product_info'

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