elasticsearch 管理常用命令集合


elasticsearch rest api遵循的格式為:

curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>

1、檢查es版本信息

curl IP:9200

2、查看集群是否健康

http://IP:9200/_cat/health?v
curl -s -XGET 'http://IP:9200/_cat/health?v'
1 綠色,最健康的狀態,代表所有的分片包括備份都可用
2 黃色,預警狀態,所有主分片功能正常,但至少有一個副本是不能正常工作的。此時集群是可以正常工作的,但是高可用性在某種程度上會受影響。
3 紅色,集群不可正常使用。某個或某些分片及其副本異常不可用,這時集群的查詢操作還能執行,但是返回的結果會不准確。對於分配到這個分片的寫入請求將會報錯,最終會導致數據的丟失。 

3、查看節點列表

http://IP:9200/_cat/nodes?v
curl 'IP:9200/_cat/nodes?v'

4、列出所有索引及存儲大小

http://IP:9200/_cat/indices?v
curl 'IP:9200/_cat/indices?v'--查詢所有索引及數據大小

5、創建索引

創建索引名為XX,默認會有5個分片,1個索引
curl -XPUT 'IP:9200/XX?pretty'

6、添加一個類型

curl -XPUT 'IP:9200/XX/external/2?pretty' -d '
{
   "gwyy": "John"
}'

7、更新一個類型

curl -XPOST 'IP:9200/XX/external/1/_update?pretty' -d '
{
   "doc": {"name": "Jaf"}
}'

8、刪除指定索引

curl -XDELETE 'IP:9200/_index?pretty' 

9、ES數據定期刪除

如果不刪除ES數據,將會導致ES存儲的數據越來越多,磁盤滿了之后將無法寫入新的數據。這時可以使用腳本定時刪除過期數據。

#/bin/bash
#es-index-clear
#只保留15天內的日志索引
LAST_DATA=`date -d "-15 days" "+%Y.%m.%d"`
#刪除上個月份所有的索引(根據自己的所有格式編寫)
curl -XDELETE 'http://ip:port/*-'${LAST_DATA}'*'

crontab -e添加定時任務:每天的凌晨一點清除索引。

0 1 * * * /search/odin/elasticsearch/scripts/es-index-clear.sh

10、查看分片狀態

curl -XGET http://localhost:9200/_cat/shards

11、查看群集狀態

curl -s -XGET 'http://localhost:9200/_cluster/stats?pretty'

12、索引管理

查詢全部索引狀態
curl 'localhost:9200/_cat/indices?v'
查詢狀態為red的索引
curl -s -XGET 'http://IP:9200/_cat/indices?health=red

13、創建索引

curl -XPUT 'localhost:9200/goods_v1?pretty' 

14、查看索引

curl '172.31.15.228:9200/goods_v1?pretty'

15、刪除索引

curl -XDELETE 'localhost:9200/goods_v1?pretty'

16、優化索引

curl -XPOST "http://127.0.0.1:9200/logstash-2015.10.28/_optimize"

curl -XGET 'http://localhost:9200/logstash-2015.10.28/_search?pretty=true' -d '
{
    "query" : {
        "matchAll" : {}
    }
}' 

索引刷新

curl -XPOST 'http://127.0.0.1:9200/index_name/_refresh'  #單個索引刷新
curl -XPOST 'http://127.0.0.1:9200/_refresh'             #全部索引刷新

flush釋放該索引所占用的內存,並將索引數據保存在磁盤

curl -XPOST 'http://127.0.0.1:9200/index_name/_flush?force'

清理緩存

curl  -XPOST 'http://127.0.0.1:9200/_cache/clear'

  

17、創建別名

curl -XPOST localhost:9200/_aliases -d '
{
    "actions": [
        { "add": {
            "alias": "goods",
            "index": "goods_v1"
        }}
    ]
}'

18、刪除並更新別名

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        { "remove" : { "index" : "goods_v2", "alias" : "goods" } },
        { "add" : { "index" : "goods_v1", "alias" : "goods" } }
    ]
}'

19、查看已有別名

curl -XGET 'localhost:9200/_cat/aliases'

20、查看別名對應的索引

curl -XGET 'localhost:9200/_alias/help'

21、查看文件描述符打開數量

curl -s -XGET '127.0.0.1:9200/_nodes/stats/process/?pretty'

22、查看分片狀態

curl -XGET "http://127.0.0.1:9200/_cat/shards"

23、查看熱點線程 

curl -XGET 'http://127.0.0.1:9200/_nodes/hot_threads'

24、針對不使用的index,進行close。我們需要的時候再進行open,可以節約內存和減輕系統的壓力

curl -XPOST 127.0.0.1:9200/index_name/_close 
curl -XPOST 127.0.0.1:9200/index_name/_open 25 18520796770

25、查看線程狀態

curl -XGET "http://127.0.0.1:9200/_nodes/thread_pool/"

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM