elasticsearch运维常用API
查看集群状态
-
查询集群状态命令:
curl -XGET "http://ip:port/_cluster/health?pretty" # ?pretty json打印结果
-
查询集群JVM状态
curl -XGET "http://ip:port/_nodes/stats/jvm?pretty" #查看具体某一个 curl -XGET "http://ip:port/_nodes/nodeName/stats/jvm?pretty"
-
查询Es全局状态:
curl -XGET "http://ip:port/_cluster/stats?pretty"
-
查看集群分配情况
curl -XGET "http://ip:port/_cluster/allocation/explain?pretty"
-
查询集群设置
curl -XGET "http://ip:port/_cluster/settings?pretty"
-
查询所有索引
# ?v 输出统计信息表头 curl -XGET "http://ip:port/_cat/indices?v" # 如果通配符过滤 curl -XGET "http://ip:port/_cat/indices/*2022*?v"
-
查看集群文档总数
curl -XGET "http://ip:port/_cat/count?v" # 仅输入`_cat/` 会返回所有可输入命令。
-
查看集群别名组
curl -XGET "http://ip:port/_cat/aliases"
-
查看当前集群索引分片信息
curl -XGET "http://ip:port/_cat/shards?v" # 注:查看某一个索引可用shards/索引名?v
-
查看集群实例存储详细信息
curl -XGET "http://ip:port/_cat/allocation?v"
-
查看当前集群的所有实例
curl -XGET "http://ip:port/_cat/nodes?v" - nodes.role角色 c : cold node d : data node f : frozen node h : hot node i : ingest node l : machine learning node m : master eligible node r : remote cluster client node s : content node t : transform node v : voting-only node w : warm node
-
查看某索引分片转移进度
curl -XGET "http://ip:port/_cat/recovery/索引名?v"
-
查看当前集群等待任务
curl -XGET "http://ip:port/_cat/pending_tasks?v"
-
查看集群写入线程池任务
curl -XGET "http://ip:port/_cat/thread_pool/bulk?v"
-
查看集群查询线程池任务
curl -XGET "http://ip:port/_cat/thread_pool/search?v"
-
查看分片未分配的原因
curl -XGET "http://ip:port/_cat/shards?v&h=index,shard,prirep,state,node,unassigned.reason" | grep UNASSIGNED
-
查看集群所有的默认配置
curl -XGET 'http://ip:port/cluster/settings?pretty&include_defaults=true&filter_path=**.index'
集群相关操作
-
设置集群分片恢复参数
curl --request PUT \ --url http://ip:port/_cluster/settings \ --header 'content-type: application/json' \ --data '{ "transient": { "cluster.routing.allocation.node_initial_primaries_recoveries": 60, "cluster.routing.allocation.node_concurrent_recoveries": 30, "cluster.routing.allocation.cluster_concurrent_rebalance": 30 } }' # 或者 curl -XPUT "http://ip:port/_cluster/settings" -H 'Content-Type: application/json' -d' { "transient": { "cluster.routing.allocation.exclude._name": "EsNode2@ip" } }'
-
根据实例名称使EsNodeX实例下线
curl --request PUT \ --url http://ip:port/_cluster/settings \ --header 'content-type: application/json' \ --data '{ "transient": { "cluster.routing.allocation.exclude._name": "EsNode2@ip" } }' # 或者 curl -XPUT "http://ip:port/_cluster/settings" -H 'Content-Type: application/json' -d' { "transient": { "cluster.routing.allocation.exclude._name": "EsNode2@ip" } }'
-
根据ip使ES数据节点下线:
curl --request PUT \ --url http://ip:port/_cluster/settings \ --header 'content-type: application/json' \ --data '{ "transient": { "cluster.routing.allocation.exclude._ip": "ip1,ip2,ip3" } }' # 或者 curl -XPUT "http://ip:port/_cluster/settings" -H 'Content-Type: application/json' -d' { "transient": { "cluster.routing.allocation.exclude._ip": "ip1,ip2,ip3" } }'
-
设置分片恢复过程中的最大带宽速度:
curl --request PUT \ --url http:/ip:port/_cluster/settings \ --header 'content-type: application/json' \ --data '{ "transient": { "indices.recovery.max_bytes_per_sec": "500mb" } }' # 或者 curl -XPUT "http://ip:port/_cluster/settings" -H 'Content-Type: application/json' -d'{ "transient":{ "indices.recovery.max_bytes_per_sec":"500mb" }}'
-
重新分配索引到 分片数为空的节点上(恢复数据)
curl --request PUT \ --url http://47.93.55.229:8118/_cluster/reroute \ --header 'content-type: application/json' \ --data '{ "commands": [ { "allocate_empty_primary": { "index": "indexname", "shard": 2, "node": "[email protected]", "accept_data_loss": true } } ] }' # 或者 curl -XPOST "http://ip:port/_cluster/reroute?pretty" -H 'Content-Type:application/json' -d '{ "commands": [{ "allocate_empty_primary": { "index": "indexname", "shard": 2, "node": "[email protected]", "accept_data_loss":true } }]}'
-
重新分配索引到 副本分片所在的节点上(恢复数据)
curl --request PUT \ --url http://47.93.55.229:8118/_cluster/reroute \ --header 'content-type: application/json' \ --data '{ "commands": [ { "allocate_stale_primary": { "index": "indexname", "shard": 2, "node": "[email protected]", "accept_data_loss": true } } ] }' # 或者 curl -XPOST "http://ip:port/_cluster/reroute?pretty" -H 'Content-Type:application/json' -d '{ "commands": [{ "allocate_stale_primary": { "index": "indexname", "shard": 2, "node": "[email protected]", "accept_data_loss":true }
-
清理ES所有缓存
curl -XPOST "http://ip:port/_cache/clear"
-
关闭分片自动平衡
curl -XPUT "http://ip:port/_cluster/settings" -H 'Content-Type:application/json' -d '{ "transient":{ "cluster.routing.rebalance.enable":"none" }}'
-
手动刷新未分配的分片
curl -XPOST "http://ip:port/_cluster/reroute?retry_failed=true"
-
关闭索引只读状态
curl -XPUT 'http://127.0.0.1:9200/_all/_settings' -H "Content-Type: application/json" -d '{"index.blocks.read_only_allow_delete": false}'
查询索引状态、信息
-
查看字符串分词情况
# 查看分词器的分词效果 curl --request POST \ --url http://47.93.55.229:8118/_analyze \ --header 'content-type: application/json' \ --data '{"analyzer":"standard","text":"这是一个测试"}' # 查看索引某个字段的分词效果 curl --request POST \ --url http://47.93.55.229:8118/indexName/_analyze \ --header 'content-type: application/json' \ --data '{"field": "title","text": "这是一个测试"}'
-
查看具体文档中某个字段的分、词情况
curl -XGET "http://ip:port/index/type/id/_termvectors?fields=${field}""
-
查询索引mapping和setting
# 查询mapping和setting curl -XGET 'http://ip:port/my_index_name?pretty' #查询setting curl -XGET 'http://ip:port/my_index_name/_settings?pretty' #查询mapping curl -XGET 'http://ip:port/my_index_name/_mappings?pretty'
-
获取索引所有的配置,包括默认的
curl -XGET 'http://ip:port/_all/_settings?include_defaults=true'
索引相关操作
-
关闭索引
curl -XPOST 'http://ip:port/my_index/_close?pretty'
-
开启索引
curl -XPOST 'http://ip:port/my_index/_open?pretty'
-
修改索引刷新时间refresh_interval
curl -XPUT 'http://ip:port/my_index/_settings?pretty' -H 'Content-Type: application/json' -d'{"refresh_interval" : "60s"}'
-
修改translog文件保留时长,默认为12小时
curl -XPUT 'http://ip:port/my_index/_settings?pretty' -H 'Content-Type: application/json' -d'{"index.translog.retention.age" : "30m"}'
-
设置索引副本:大量写数据,刷数据,可以临时关闭副本,写完再开启
curl -XPUT 'http://ip:port/my_index/_settings?pretty' -H 'Content-Type: application/json' -d'{"number_of_replicas" : 1}'
-
执行refresh,将内存数据刷新到磁盘缓存
curl -XPOST 'http://ip:port/myindex/_refresh'
-
执行flush,将磁盘缓存刷新到文件系统
curl -XPOST 'https://ip:port/myindex/_flush'
-
执行synced flush,生成syncid
curl -XPOST 'http://ip:port/_flush/synced'
-
强制执行段合并
curl -XPOST 'http://ip:port/myindex/_forcemerge?only_expunge_deletes=false&max_num_segments=1&flush=true&pretty'
-
设置索引在每个esnode上的分片个数
curl -XPUT 'http://ip:port/myindex/_settings?pretty' -H 'Content-Type: application/json' -d'{"index.routing.allocation.total_shards_per_node" : "2"}'
-
配置控制段合并的refresh、merge线程数等
curl --request PUT \ --url 'http://ip:port8/my_index/_settings?pretty=' \ --header 'content-type: application/json' \ --data '{ "refresh_interval": "60s", "merge": { "scheduler": { "max_merge_count": "100", "max_thread_count": "1" }, "policy": { "segments_per_tier": "100", "floor_segment": "1m", "max_merged_segment": "2g" } } }' # 或者 curl -XPUT "http://ip:port/my_index/_settings?pretty" -H 'Content-Type: application/json' -d'{"refresh_interval": "60s","merge":{"scheduler":{"max_merge_count" : "100","max_thread_count" : "1"},"policy":{"segments_per_tier" : "100","floor_segment" : "1m","max_merged_segment" : "2g"}}}'
-
设置索引的刷新时间和translog配置参数
# 设置translog参数,必须先关闭索引,设置完成后再打开 curl --request PUT \ --url 'http://47.93.55.229:8118/my_index/_settings?pretty=' \ --header 'content-type: application/json' \ --data '{ "index": { "refresh_interval": "60s", "translog": { "flush_threshold_size": "1GB", "sync_interval": "120s", "durability": "async" } } }' # 或者 curl -XPUT "http://ip:httpport/*/_settings" -H 'Content-Type: application/json' -d'{ "index":{ "refresh_interval" : "60s","translog":{ "flush_threshold_size": "1GB", "sync_interval": "120s", "durability": "async"}}}'