新版Elasticsearch 7.x 命令行快速入門

關於Elasticsearch 的介紹,可以參考之前的文章:



一、_cat 命令

_cat命令是系統信息查詢相關的,下面分成好幾個子命令分類。

curl http://localhost:9200/_cat

/_cat/allocation

/_cat/shards

/_cat/shards/{index}

/_cat/master

/_cat/nodes

/_cat/tasks

/_cat/indices

/_cat/indices/{index}

/_cat/segments

/_cat/segments/{index}

/_cat/count

/_cat/count/{index}

/_cat/recovery

/_cat/recovery/{index}

/_cat/health

/_cat/pending_tasks

/_cat/aliases

/_cat/aliases/{alias}

/_cat/thread_pool

/_cat/thread_pool/{thread_pools}

/_cat/plugins

/_cat/fielddata

/_cat/fielddata/{fields}

/_cat/nodeattrs

/_cat/repositories

/_cat/snapshots/{repository}

/_cat/templates


curl http://localhost:9200/_cat/nodes?v

顯示Elasticsearch 集群節點的詳細信息,如下圖所示。

新版Elasticsearch 7.x 命令行快速入門


二、索引相關操作

ES中一個索引就類似於數據庫中一個數據庫的概念。當然ES中的數據支持結構化、非結構化數據。

先查詢ES集群中有哪些索引:

curl http://localhost:9200/_cat/indices?v

新版Elasticsearch 7.x 命令行快速入門


(1)創建新索引

創建一個新索引-demo,然後再次查詢索引列表。這時,已經有新增的索引demo了。

curl -XPUT localhost:9200/demo

新版Elasticsearch 7.x 命令行快速入門


再看看ES的數據目錄裡是不是創建了相應的文件:

ls data\\nodes\\0\\indices\\

新版Elasticsearch 7.x 命令行快速入門

其中 data 是 ES 集群的數據所在目錄,nodes 下是集群的節點列表,有多少個節點就有多少個子目錄,這裡演示系統只有一個名稱為0的子目錄,說明nodes 下只有一個節點,如果再增加一臺服務器,就會有0和1兩個子目錄。


ikxY1EscQ0SgbFxrp-X-qw 是demo索引的uuid,裡面有0-4 個子目錄,代表了5個分片。ES 默認對每個索引分配5個分片(shard),每個分片裡數據在進行分段(segment),數據會保存在分段裡面。

新版Elasticsearch 7.x 命令行快速入門


segments_3 就是一個分段,數據會保存在分段裡面。如果是集群配置,則每個分片會根據配置參數會存在多個副本(replica)。

新版Elasticsearch 7.x 命令行快速入門


當索引中的數據(也就是文檔)量超過原定的分片容量時,無法保存新的數據,這時候ES要進行數據遷移,數據遷移一般是新建一個索引,給這個新索引分配足夠的分片數,再將數據移到新索引中。新索引的創建是很消耗資源的,因此最好在創建一個索引前預先計算好容量需求。


(2)新建文檔

在索引 demo 中,創建第一個文檔。

新版Elasticsearch 7.x 命令行快速入門


(3)查詢索引

查詢整個索引的情況。

curl http://localhost:9200/demo?pretty

新版Elasticsearch 7.x 命令行快速入門

這個索引demo包含5個分片,副本數為1。



還可以用_search命令來查詢索引裡的具體文檔,不帶任何參數查詢所有文檔。

curl http://localhost:9200/demo/_search?pretty

新版Elasticsearch 7.x 命令行快速入門


根據主鍵查詢文檔:

curl http://localhost:9200/demo/_doc/1?pretty

新版Elasticsearch 7.x 命令行快速入門

_version=1說明這個主鍵的內容被更新了1次。


下面 found = false,表示沒有找到該主鍵的文檔。

新版Elasticsearch 7.x 命令行快速入門


(4)更新文檔

更新文檔之後,版本號 version 會加1。

新版Elasticsearch 7.x 命令行快速入門


(5)刪除文檔

curl -XDELETE localhost:9200/demo/_doc/1?pretty

新版Elasticsearch 7.x 命令行快速入門


刪除文檔之後,再查詢主鍵=1的文檔,發現已經找不到了。

curl localhost:9200/demo/_doc/1

{"_index":"demo","_type":"_doc","_id":"1","found":false}



三、查詢條件

用_search子命令來查詢文檔,query參數可指定查詢條件。

curl http://localhost:9200/demo/_search?pretty

不給參數,會輸出所有文檔。


其中,Hits是統計信息,給出文檔總數:

新版Elasticsearch 7.x 命令行快速入門


查詢字段名name中包含rickie的記錄。

新版Elasticsearch 7.x 命令行快速入門


如果加上”_source”:false參數,則查詢結果不顯示文檔內容,只顯示文檔序號。

新版Elasticsearch 7.x 命令行快速入門

指定某些字段,比如只需要返回”name”字段。

新版Elasticsearch 7.x 命令行快速入門



四、用_all查詢所有索引

curl http://localhost:9200/_all?pretty

返回結果包括了索引中的類別列表。Setting裡是索引的元數據信息,分配了5個分片。

新版Elasticsearch 7.x 命令行快速入門


分享到:


相關文章: