@[ElasticSearch]
REST API
現在我們已經運行了節點和集羣,下一步就是理解如何連接Elasticsearch
。Elasticsearch
提供了全面和強大的REST API
來跟Elasticsearch
集羣相互作用。我們可以用REST API
來做一下這些事情:
- 檢查集羣,節點和索引的健康,狀態和統計信息;
- 管理集羣,節點和索引的數據和元數據;
- 對索引執行
CRUD (Create, Read, Update, and Delete)
和搜索操作; - 執行高級搜索操作,例如分頁,排序,過濾,腳本,聚合等等。
集羣健康
開始基本的健康檢查,可以查看集羣在做什麼。本文使用curl
進行操作,但是你可以使用任何工具來進行調用REST/HTTP
請求。假設我們仍然在上文提到的運行着Elasticsearch
的節點上,重新打開一個終端窗口。
我們可以使用_cat API
來檢查集羣的健康,在這之前應當確認我們的節點在http
的9200端口可以訪問:
curl 'localhost:9200/_cat/health?v'
我們可以得到如下的返回結果:
epoch timestamp cluster status node.total node.data shards pri relo init unassign
1394735289 14:28:09 elasticsearch green 1 1 0 0 0 0 0
我們可以看到我們的名字爲elasticsearch
的集羣的狀態爲green
。
集羣健康分爲三種,green
,yellow
,red
。Green的意思是所有狀態都很好(集羣功能全部可用),Yellow的意思是所有的數據都可以使用,但是某些副本沒有被分配(集羣功能全部可用),Red的意思是某些數據不可用。需要注意的是,即使狀態爲red,也有一部分數據可,但是你需要儘快修復它,因爲你有丟失數據。
從上面的返回結果中我們可以看到節點數目只有一個,分片數目爲0,因爲我們還沒有分配數據。需要注意的一點,因爲我們使用了默認的集羣名稱(elasticsearch
),而且Elasticsearch
默認使用單播網絡發現同一臺機器中的節點,很有可能你會啓動不僅僅一個節點。在這種情況下,你可能得到不僅僅一個節點。
我們也可以使用下面的命令得到節點列表:
curl 'localhost:9200/_cat/nodes?v'
下面是返回結果:
curl 'localhost:9200/_cat/nodes?v'
host ip heap.percent ram.percent load node.role master name
mwubuntu1 127.0.1.1 8 4 0.00 d * New Goblin
我們可以看到我們唯一的節點命名爲New Goblin
。
列出所有的索引
現在我們可以看看我們的索引:
curl 'localhost:9200/_cat/indices?v'
返回結果爲:
curl 'localhost:9200/_cat/indices?v'
health index pri rep docs.count docs.deleted store.size pri.store.size
上面說明我們現在還沒有索引。
創建索引
現在我們創建個名字爲customer
的索引重新列出所有的索引:
curl -XPUT 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'
第一條命令使用PUT
創建一個名字爲customer
的索引,我們添加pretty
參數的原因是將結果打印成爲JSON
返回:
下面是結果:
curl -XPUT 'localhost:9200/customer?pretty'
{
"acknowledged" : true
}
curl 'localhost:9200/_cat/indices?v'
health index pri rep docs.count docs.deleted store.size pri.store.size
yellow customer 5 1 0 0 495b 495b
第二條結果告訴我們我們現在有一個名爲customer
的索引,它有5個主要分片和1個複製分片(均爲默認),它沒有包含任何的文檔。
你肯定觀察到了customer
索引的狀態爲yellow,我們之前討論過,yellow意味着複製分片有一部分沒有分配。這裏的原因是默認索引有一個複製分片,但是我們只有一個節點,而複製節點不會被分配到跟主要節點在同一個節點中。當有其他節點加入集羣,複製分片被分配以後,yellow狀態就會發生改變,成爲green。
索引和查詢文檔
現在我們可以放一些數據到我們的索引中了,前文提到過,在索引文檔之前,我們應當先指定類型。
我們現在索引一條數據,在customer
索引中,external
類型,文檔的ID
爲1:
我們的JSON
文檔爲:{ "name": "John Doe" }
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
返回結果爲:
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"created" : true
}
從上面的結果中我們看到,一條customer
文檔成功的插入到了customer
索引的external
類型中,文檔還有一個我們在索引過程中指定的一個內部ID
爲1。
Elasticsearch
不需要你在索引數據的時候顯性的創建一個索引,在上面的例子中,Elasticsearch
可以自動的創建customer
索引,如果之前不存在的話。
現在我們可以取回我們剛索引的文檔:
curl -XGET 'localhost:9200/customer/external/1?pretty'
返回結果爲:
curl -XGET 'localhost:9200/customer/external/1?pretty'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : { "name": "John Doe" }
}
found
字段說明我們用ID
1找到了這條文檔,_source
字段返回了之前我們索引的整條JSON
文檔。
刪除索引
現在刪除我們剛纔建立的索引,然後列出所有的索引:
curl -XDELETE 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'
返回結果爲:
curl -XDELETE 'localhost:9200/customer?pretty'
{
"acknowledged" : true
}
curl 'localhost:9200/_cat/indices?v'
health index pri rep docs.count docs.deleted store.size pri.store.size
結果說明索引刪除成功,我們現在集羣中沒有任何的索引了。
在進行下面的之前,我們可以回顧一下我們目前學到的API
:
curl -XPUT 'localhost:9200/customer'
curl -XPUT 'localhost:9200/customer/external/1' -d '
{
"name": "John Doe"
}'
curl 'localhost:9200/customer/external/1'
curl -XDELETE 'localhost:9200/customer'
我們仔細看上面的命令,會發現我們訪問Elasticsearch
數據的命令模式,可以總結爲:
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
這種REST
訪問模式在API
中很常見,因此如果你可以記住的話,你將會在掌握Elasticsearch
過程中有一個良好的開始。