Elasticsearch 6.1官方參考手冊(一)入門(3)集羣入門學習

探究你的集羣

REST API

既然咱們的節點(集羣)已經安裝成功而且已經啓動運行,那麼下一步就是去了解如何去操做它。幸運的是,Elasticsearch提供了很是全面和強大的REST API,咱們能夠經過它去跟集羣交互。經過API咱們能夠完成以下的功能:html

  • 檢查集羣,節點和索引的健康情況,狀態和統計數據
  • 管理集羣,節點和索引的數據和原數據
  • 執行CRUD(增刪改查)操做,依靠索引進行搜索
  • 執行高級搜索操做,好比分頁,排序,過濾,腳本化,彙集等等

集羣健康監控

讓咱們從一個簡單的健康檢查開始,經過這個咱們能夠了解咱們集羣的運行狀況。咱們將使用curl工具來作這個測試,固然你可使用任何能夠發送HTTP/REST請求的工具。讓咱們假設咱們依然在以前已啓動的Elasticsearch節點上而且打開了另外一個shell窗口。node

咱們將使用 _cat API 去檢查集羣健康狀態。HTTP請求內容爲:shell

GET /_cat/health?v

你能夠經過點擊VIEW IN ConsoleKibana Console中運行命令,或者直接執行以下curl命令:json

curl -XGET 'localhost:9200/_cat/health?v&pretty'

響應結果爲:網絡

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1475247709 17:01:49  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%

咱們能夠看到咱們的名稱爲「elasticsearch」的集羣正在運行,狀態標識爲greenapp

不管什麼時候查看集羣健康狀態,咱們會獲得greenyellowred中的任何一個。curl

  • Green - 一切運行正常(集羣功能齊全)
  • Yellow - 全部數據是能夠獲取的,可是一些複製品尚未被分配(集羣功能齊全)
  • Red - 一些數據由於一些緣由獲取不到(集羣部分功能不可用)

注意:當一個集羣處於red狀態時,它會經過可用的分片繼續提供搜索服務,可是當有未分配的分片時,你須要儘快的修復它。elasticsearch

另外,從上面的返回結果中咱們能夠看到,當咱們裏面沒有數據時,總共有1個節點,0個分片。注意當咱們使用默認的集羣名稱(elasticsearch)而且當Elasticsearch默認使用單播網絡發如今同一臺機器上的其它節點時,極可能你會在你電腦上不當心啓動不止一個節點而且他們都加入了一個集羣。在這種狀況下,你可能會從上面的返回結果中看到不止一個節點。ide

咱們也能夠經過以下請求獲取集羣中的節點列表:工具

Http請求體

GET /_cat/nodes?v

Curl命令

curl -XGET 'localhost:9200/_cat/nodes?v&pretty'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from= https://www.elastic.co/guide/...

返回結果爲:

ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           10           5   5    4.46                        mdi      *      PB2SGZY

這裏,咱們能夠看到咱們的一個節點名稱叫作「PB2SGZY」,它是目前咱們集羣中的惟一的節點。

列出全部的索引

如今讓咱們來大概看一看咱們的索引:
Http請求內容:

GET /_cat/indices?v

Curl命令

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from= https://www.elastic.co/guide/...

獲得的返回結果爲:

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

這個返回結果只是一個表頭,簡單說就是咱們的集羣中尚未任何索引。

建立一個索引

如今讓咱們建立一個索引,名稱爲「customer」,而後再一次列出全部的索引:

Http請求內容:

PUT /customer?pretty

GET /_cat/indices?v

Curl命令

curl -XPUT 'localhost:9200/customer?pretty&pretty'

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from= https://www.elastic.co/guide/...

第一個命令使用PUT方法建立了一個名爲「customer」的索引。咱們簡單的在請求後面追加pretty參數來使返回值以格式化過美觀的JSON輸出(若是返回值是JSON格式的話)。

而後它的返回結果爲:

第一個命令:
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}
第二個命令:
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer 95SQ4TSUT7mWBT7VNHH67A   5   1          0            0       260b           260b

第二個命令的返回結果告訴咱們,咱們如今有1個名稱爲「customer」的索引,而且有5個主分片和1個拷貝(默認狀況),而且裏面包含0個文檔。

你可能也注意到,這個customer索引的健康狀態是yellow,回憶咱們以前討論過的,yellow的意思是有一些拷貝尚未被分配。索引起生這種狀況的緣由是Elasticsearch默認爲當前索引建立一個拷貝。可是當前咱們只啓動了一個節點,這個拷貝直到一段時間後有另外一個節點加入集羣以前,不會被分配(爲了高可用,拷貝不會與索引分配到同一個節點上)。一旦拷貝在第二個節點上得到分配,這個索引的健康狀態就會變成green。

索引和文檔查詢

如今讓咱們往customer索引中放點東西。以下請求將一個簡單的顧客文檔放入customer索引中,這個文檔有一個ID爲1:

Http請求內容:

PUT /customer/doc/1?pretty
{
  "name": "John Doe"
}

Curl命令

curl -XPUT 'localhost:9200/customer/doc/1?pretty&pretty' -H 'Content-Type: application/json' -d '{"name": "John Doe"}'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_index_and_query_a_document/1.json

返回結果爲:

{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

從上面咱們能夠看到,一個新的顧客文檔已經在customer索引中成功建立。同時這個文檔有一個本身的id,這個id就是咱們在將文檔加入索引時指定的。

這裏有一個重要的注意點,你不須要在將一個文檔加入一個索引前明確的將這個索引預先建立好。在上面咱們建立文檔的例子中,若是這個customer索引事先不存在,Elasticsearch會自動建立customer索引。

如今讓咱們獲取剛剛加入索引的文檔:

Http請求體:

GET /customer/doc/1?pretty

Curl命令

curl -XGET 'localhost:9200/customer/doc/1?pretty&pretty'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_index_and_query_a_document/2.json

返回結果爲:

{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : { "name": "John Doe" }
}

這裏沒有什麼不尋常的,除了一個屬性found,這個found屬性表示咱們經過請求ID爲1發現了一個文檔,還有另外一個屬性_source,_source屬性返回咱們在上一步中加入索引的完整JSON文檔內容。

刪除一個索引

如今讓咱們刪除剛剛建立的索引而且再次列出全部的索引:

Http請求內容:

DELETE /customer?pretty
GET /_cat/indices?v

Curl命令

curl -XDELETE 'localhost:9200/customer?pretty&pretty'
curl -XGET 'localhost:9200/_cat/indices?v&pretty'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_delete_an_index/1.json

第一個命令的返回結果爲:

{
  "acknowledged" : true
}

第二個命令的返回結果爲:

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

以上結果意味着咱們的索引已經被刪除,而且咱們回到了剛開始集羣中什麼都沒有的地方。

在咱們繼續前進以前,讓咱們來仔細看一下到目前爲止學習的這些API命令:

PUT /customer
PUT /customer/doc/1
{
  "name": "John Doe"
}
GET /customer/doc/1
DELETE /customer

若是咱們在學習上面的命令時很是仔細的話,咱們必定會發如今Elasticsearch中訪問數據的模式。這個模式能夠總結爲如下形式:

<REST Verb> /<Index>/<Type>/<ID>

這種REST訪問模式遍及全部的API命令,若是簡單的記住它,你將會在掌握Elasticsearch的過程當中有一個很好的開端。

以下是我在上述章節實際作的操做:

[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/health?v&pretty'
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1514269983 14:33:03  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/health?v&pretty'
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1514270109 14:35:09  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/nodes?v&pretty'
ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1            7          93   0    0.00    0.01     0.05 mdi       *      sEicoNR
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/indices?v&pretty'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
[root@bogon elasticsearch-6.1.1]# curl -XPUT 'localhost:9200/customer?pretty&pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/indices?v&pretty'
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer Azxs-a4FQnGKgAj0zdWXxQ   5   1          0            0      1.1kb          1.1kb
[root@bogon elasticsearch-6.1.1]# curl -XPUT 'localhost:9200/customer/doc/1?pretty&pretty' -H 'Content-Type: application/json' -d '{"name": "John Doe"}'
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/customer/doc/1?pretty&pretty'
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "John Doe"
  }
}
[root@bogon elasticsearch-6.1.1]# curl -XDELETE 'localhost:9200/customer?pretty&pretty'
{
  "acknowledged" : true
}
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/indices?v&pretty'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
[root@bogon elasticsearch-6.1.1]#
相關文章
相關標籤/搜索