The REST API
Now that we have our node (and cluster) up and running, the next step is to understand how to communicate with it.
1.Check your cluster, node, and index health, status, and statistics
2.Administer your cluster, node, and index data and metadata
3.Perform CRUD (Create, Read, Update, and Delete) and search operations against your indexes
4.Execute advanced search operations such as paging, sorting, filtering, scripting, aggregations, and many others
Cluster Health
es@qiandu1-yuwencai:~/cluster/elasticsearch-2.3.1$ curl 'localhost:9200/_cat/health?v'
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1460959146 13:59:06 es_cluster green 1 1 0 0 0 0 0 0 - 100.0%
status
- green, Green means everything is good (cluster is fully functional)
- yellow, yellow means all data is available but some replicas are not yet allocated (cluster is fully functional)
- red, Red means some data is not available for whatever reason
List All Nodes
es@qiandu1-yuwencai:~/cluster/elasticsearch-2.3.1$ curl 'localhost:9200/_cat/nodes?v'
host ip heap.percent ram.percent load node.role master name
127.0.0.1 127.0.0.1 7 82 0.01 d * es_node_1
表明集群中只有一个节点,es_node_1
List All Indices
es@qiandu1-yuwencai:~/cluster/elasticsearch-2.3.1$ curl 'localhost:9200/_cat/indices?v'
health status index pri rep docs.count docs.deleted store.size pri.store.size
表明集群中还没有indices
Create an Index
es@qiandu1-yuwencai:~/cluster/elasticsearch-2.3.1$ curl -XPUT 'localhost:9200/customer?pretty'
返回·
{
"acknowledged" : true
}
The first command creates the index named "customer" using the PUT verb. We simply append pretty to the end of the call to tell it to pretty-print the JSON response (if any).
es@qiandu1-yuwencai:~/cluster/elasticsearch-2.3.1$ curl 'localhost:9200/_cat/indices?v'
返回
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open customer 5 1 0 0 650b 650b
The results of the second command tells us that we now have 1 index named customer and it has 5 primary shards and 1 replica (the defaults) and it contains 0 documents in it.
The results of the second command tells us that we now have 1 index named customer and it has 5 primary shards and 1 replica (the defaults) and it contains 0 documents in it.
Status yellow means that some replicas are not (yet) allocated. The reason this happens for this index is because Elasticsearch by default created one replica for this index. Since we only have one node running at the moment, that one replica cannot yet be allocated (for high availability) until a later point in time when another node joins the cluster. Once that replica gets allocated onto a second node, the health status for this index will turn to green.
Index and Query a Document
Let’s now put something into our customer index. Remember previously that in order to index a document, we must tell Elasticsearch which type in the index it should go to.
Let’s index a simple customer document into the customer index, "external" type, with an ID of 1 as follows:
Our JSON document: { "name": "John Doe" }
es@qiandu1-yuwencai:~/cluster/elasticsearch-2.3.1$curl -XPUT 'localhost:9200/customer/external/1?pertty' -d '
{
"name":"John Doe"
}'
返回
{"_index":"customer","_type":"external","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}
From the above, we can see that a new customer document was successfully created inside the customer index and the external type. The document also has an internal id of 1 which we specified at index time.
Retrieve Document
es@qiandu1-yuwencai:~/cluster/elasticsearch-2.3.1$ curl -XGET 'localhost:9200/customer/external/1?pretty'
返回
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "John Doe"
}
}
- field found:stating that we found a document with the requested ID 1
- field _source: which returns the full JSON document that we indexed from the previous step.
Delete an Index
es@qiandu1-yuwencai:~/cluster/elasticsearch-2.3.1$ curl -XDELETE 'localhost:9200/customer?pertty'
返回
{"acknowledged":true}
查看索引
es@qiandu1-yuwencai:~/cluster/elasticsearch-2.3.1$ curl 'localhost:9200/_cat/indices?v'
返回
health status index pri rep docs.count docs.deleted store.size pri.store.size
Which means that the index was deleted successfully and we are now back to where we started with nothing in our cluster.
REST API Summary
That pattern can be summarized as follows:
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
参考资料
【1】
来源: https://www.elastic.co/guide/en/elasticsearch/reference/current/_exploring_your_cluster.html
【2】
来源: https://www.elastic.co/guide/en/elasticsearch/reference/current/_cluster_health.html
【3】
来源: https://www.elastic.co/guide/en/elasticsearch/reference/current/_create_an_index.html
【4】
来源: https://www.elastic.co/guide/en/elasticsearch/reference/current/_index_and_query_a_document.html