A. es 操作
1. 檢查 es 集群健康狀態
bash命令:curl -XGET 'localhost:9200/_cat/health?v&pretty'
kibana命令:GET /_cat/health?v
返回示例:

描述:可以看到紅框范圍內的值為 yellow,它代表了我們 es 服務集群的健康狀態,詳細描述如下。解讀后我們可以了解到,我們的 yellow 狀態對於日常使用沒有影響,它只是因為我們的集群暫時由單節點服務器組成,沒有多余的節點分配給我們的分片副本了,解決示例會在以后的文章中給出。
-
RED: Damnit. Some or all of (primary) shards are not ready.
-
YELLOW: Elasticsearch has allocated all of the primary shards, but some/all of the replicas have not been allocated.
-
GREEN: Great. Your cluster is fully operational. Elasticsearch is able to allocate all shards and replicas to machines within the cluster.
2. 獲取集群中的節點列表
bash命令:curl -XGET 'localhost:9200/_cat /nodes?v?pretty'
kibana命令:GET /_cat/nodes?v
返回示例:

描述:注意最后的 name 即為我們某節點的唯一名稱
以下描述中
所有的 kibana 命令都具有<REST HttpVerb> /<Index>/<Type>/<ID> <?pretty>格式
所有的 bash Curl 命令都具有curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'格式,請結合實際語句進行區分。
3. 創建索引
bash命令: curl -XPUT 'localhost:9200/customer?pretty&pretty'
kibana命令:PUT /customer?pretty
返回示例:
{
"acknowledged": true,
"shards_acknowledged": true
}
4. 獲取索引
bash命令:curl -XGET 'localhost:9200/_cat/indices?v&pretty'
kibana命令:GET /_cat/indices?v
返回示例:

描述: 該條指令用於獲取所有索引列表
5. 索引文檔
bash命令:
curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
kibana命令:
PUT /customer/external/1?pretty
{
"name": "John Doe"
}
返回示例:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
描述:索引中可以存在不同的類型,我們剛剛便創建了類型 “external”及其文檔,大家可以把它理解為關系型數據庫中的表和列。索引時 ID 字段是可選的,假如我們沒有指定,es 將自動為我們生成 ID(此種情況下需要使用 POST HTTPVerb)。
6. 查詢文檔
bash命令:curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'
kibana命令:GET /customer/external/1?pretty
返回示例:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "John Doe"
}
}
描述: 簡單查詢格式一般為 /index/type/id
7. 刪除索引
bash命令:curl -XDELETE 'localhost:9200/customer?pretty&pretty'
kibana命令:DELETE /customer?pretty
返回示例:
{
"acknowledged": true
}
描述: 通過添加 * 通配符,我們可以刪除所有形如 customer2017-3-8-11-26-58的索引。
8. 更新文檔
bash命令:
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}
'
kibana命令:
POST /customer/external/1/_update?pretty
{
"doc": { "name": "Jane Doe", "age": 20 }
}
返回示例:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 7, //修改次數
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
描述: 我們剛才針對之前錄入的 customer 的某條 id 為 1 的數據進行了更新,並擴充了其屬性。值得注意的是,當我們執行更新操作時,es 實際上是對索引的文檔進行了刪除並重建的操作,並不是真正意義上的更新。
9. 刪除文檔
bash命令: curl -XDELETE 'localhost:9200/customer/ external/2?pretty?pretty'
kibana命令:DELETE /customer/external/2?pretty
返回示例:
{
"found": true,
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 8,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
10. 批量查詢文檔
bash命令: curl -XGET 'localhost:9200/customer/ external/_search?pretty'
kibana命令:GET /customer/external/_search?pretty
返回示例:
{
"took": 18,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "customer",
"_type": "external",
"_id": "AVqm6MRTU67sF7xAeJ5R",
"_score": 1,
"_source": {
"name": "John Doe"
}
},
{
"_index": "customer",
"_type": "external",
"_id": "AVqm6MURU67sF7xAeJ5S",
"_score": 1,
"_source": {
"name": "Jane Doe"
}
}
]
}
}
描述: 剛才演示的都是根據 ID 獲取單條數據,但是如果 ID 是自動生成值,這樣的方式就不十分友好了,所以 es 提供了 _search 關鍵字來進行對索引類型中所有資源的獲取操作,默認獲取前十條匹配信息。其實有心的讀者應該也注意到剛才我們在進行 update 操作時,指令中也有 _update 關鍵字,而在 kibana 的控制台中,我們還能通過它的智能提示獲取更多這樣的簡便操作指令。如 _count,_create等。后續也將介紹使用匹配規則來查找特定的文檔。
11. 字符串查詢文檔
bash命令: curl -XGET 'localhost:9200/customer/external/_search?q=name:Jane'
kibana命令:GET /customer/external/_search?q=name:Jane Doe?pretty
返回示例: 暫略
描述: 字符串查詢即是一種條件查詢,q=name:Jane 即意味着我們想要查詢 external 類型中屬性 name 值含有 Jane 的文檔,es 會自動將相關匹配返回給我們。假如想要了解更多,請參見 Simple Query String Query。
12. DSL條件查詢文檔
bash命令:
curl -XGET 'localhost:9200/customer/external/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match" : {
"name":"Jane"
}
}
}
'
kibana命令:
GET /customer/external/_search?pretty
{
"query": {
"match" : {
"name":"Joe"
}
}
}
返回示例: 暫略
描述: DSL 被稱為特定領域語言,如 T-SQL 就是一種 DSL。它提供了更豐富更強大的方法供給開發者使用,如以上代碼和之前的字符串查詢的含義便是相同,更多用法詳見 Query-DSL。
13. 批量更新文檔
bash命令:
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d'
{"index":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
{"name": "John Doe" }
{"index":{"_id":"AVqm6MURU67sF7xAeJ5S"}}
{"name": "Jane Doe" }
{"update":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"AVqm6MURU67sF7xAeJ5S"}}
'
kibana命令:
POST /customer/external/_bulk?pretty
{"index":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
{"name": "John Doe" }
{"index":{"_id":"AVqm6MURU67sF7xAeJ5S"}}
{"name": "Jane Doe" }
{"update":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"AVqm6MURU67sF7xAeJ5S"}}
返回示例:
{
"took": 30,
"errors": false,
"items": [
{
"index": {//ignore},
"created": false,
"status": 200
}
},
{
"index": {//ignore},
"created": true,
"status": 201
}
},
{
"update": {//ignore},
"status": 200
}
},
{
"delete": {//ignore},
"status": 200
}
}
]
}
描述: 不要被這么“多”的命令嚇到了,其實我們仔細看下來,就會發現筆者只是讓 es 執行了添加 id 為 xx 和 yy 的文檔,然后再更新 id 為 xx 的文檔內容,最后刪除了 id 為 yy 的文檔。一步到位,順序執行,更不會因為中間某步出了錯誤便停止運行,所以有關於這個特性,希望大家在執行命令的時候要特別注意。
B. 結尾
通過上文的描述,我們已經初步了解了 es 中對於文檔,類型和索引的相關操作,但是要注意,我們的努力仍然是十分粗糙的。更多的詳細操作,大家可以參考官網的 api 文檔,里面提到了本文省略的聚合,過濾條件查詢和批量刪除等十分有效的 api。
筆者之所以沒提出省略的部分,只是因為這樣編寫文章的工程量將會比較繁重,而且考慮到開發者平時更多地會使用客戶端進行遠程操作,精通開發平台內專業工具的使用就已經足夠應付大多數的需求了。
