Elasticsearch常用命令总结
- 查看集群健康状态
- green:每个索引的primary shard 和replica 都是active状态,ES集群正常。
- yellow:每个索引的primary shard 是 active状态,但是部分的replica shard 不是active,ES集群可以正常使用。
- red:不是所有索引的primary shard 都是active状态,部分索引数据丢失,集群不可用。
可以通过以下方式查看:
http://127.0.0.1:9200/_cluster/health?pretty#
{
"cluster_name" : "elasticsearch",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 8,
"number_of_data_nodes" : 8,
"active_primary_shards" : 373,
"active_shards" : 752,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
- 各节点使用情况
http://192.168.2.131:9200/_cat/nodes?v&s=ip:desc&h=http,version,jdk,disk.total,disk.used,disk.avail,disk.used_percent,heap.current,heap.percent,heap.max,ram.current,ram.percent,ram.max,master
3. 查询ES的索引(按主分片大小排序)
http://192.168.2.131:9200/_cat/indices?v&s=pri.store.size:desc
4. 查询ES节点分配情况
http://192.168.2.131:9200/_cat/allocation?v
5. hot threads 查询,查询ES的性能到底是什么原因引起的
http://192.168.2.131:9200/_nodes/node131/hot_threads
- 设置索引的慢查询记录: 将大于2秒的查询记录到日志中,将大于800ms的 fetch记录到日志中
Curl -u elastic:'password' -XPUT 192.168.2.131:9200/contractcenter/_settings -H 'Content-Type: application/json' -d '{
"index.search.slowlog.threshold.query.warn": "5s",
"index.search.slowlog.threshold.query.info": "2s",
"index.search.slowlog.threshold.query.debug": "600ms",
"index.search.slowlog.threshold.query.trace": "200ms",
"index.search.slowlog.threshold.fetch.warn": "1s",
"index.search.slowlog.threshold.fetch.info": "800ms",
"index.search.slowlog.threshold.fetch.debug": "500ms",
"index.search.slowlog.threshold.fetch.trace": "10ms",
"index.search.slowlog.level": "info"
}'
- 强制进行 segment 合并
curl -u elastic:'password' -XPOST 'http:// 192.168.2.131:9200/contractcenter/_forcemerge?only_expunge_deletes=true'
- 查看线程池
http://192.168.2.131:9200/_cat/thread_pool?v
- 重新分配失败的分片(默认索引的尝试次数为5)
curl -u elastic:'password' -XPOST 'http://192.168.2.131:9200/_cluster/reroute?retry_failed=true'
- 分片分配设置
cluster.routing.allocation.enable参数为 null时,允许进行分片分配;该参数为primaries时,仅允许分配主分片的分片;该参数为none时,禁用所有分片分配,如下所示:
curl -u elastic:'password' -X PUT "192.168.2.131:9200/_cluster/settings" -H 'Content-Type: application/json' -d '{ "persistent": { "cluster.routing.allocation.enable": "none" } }'
11.移除某个节点上的全部分片
curl -u elastic:'password' -XPUT http://192.168.2.131:9200/_cluster/settings?pretty -H 'Content-Type: application/json' -d '
{"transient":{"cluster.routing.allocation.exclude._ip":"192.168.2.132"}}'
12.查看分片未分配的原因
http://192.168.2.131:9200/_cat/shards?v&h=n,index,shard,prirep,state,sto,sc,unassigned.reason,unassigned.details
ALLOCATION_FAILED:由于分片分配失败而未分配。
CLUSTER_RECOVERED:由于集群恢复而未分配。
DANGLING_INDEX_IMPORTED:由于导入了悬空索引导致未分配。
EXISTING_INDEX_RESTORED:由于恢复为已关闭的索引导致未分配。
INDEX_CREATED:由于API创建索引而未分配。
INDEX_REOPENED:由于打开已关闭索引而未分配。
NEW_INDEX_RESTORED:由于恢复到新索引而未分配。
NODE_LEFT:由于托管的节点离开集群而未分配。
REALLOCATED_REPLICA:确定了更好的副本位置,并导致现有副本分配被取消。
REINITIALIZED:当分片从开始移动回初始化,导致未分配。
REPLICA_ADDED:由于显式添加副本而未分配。
REROUTE_CANCELLED:由于显式取消重新路由命令而未分配。
13.移动指定节点的某个分片到另一个节点
curl -u elastic:'password' -XPOST 'http://192.168.2.131:9200/_cluster/reroute' -H 'Content-Type: application/json' -d '{ "commands" : [ { "move" : { "index" : “index", "shard" : 1, "from_node" : "192.168.2.131", "to_node" : "192.168.2.132"
}
}
]
}'
14.磁盘高低水位在线调整
高水位参数:cluster.routing.allocation.disk.watermark.high
低水位参数:cluster.routing.allocation.disk.watermark.low
实例:调整高水位阈值
curl -H "Content-Type:application/json" -u elastic -XPUT "http://192.168.2.131:9200/_cluster/settings" -d '{
"transient": {
"cluster.routing.allocation.disk.watermark.high":"95%"
} }'
15.reindex复制索引数据
curl -u elastic:'password' -XPOST 'http://192.168.2.131:9200/_reindex?slices=5&refresh' -H 'Content-Type: application/json' -d {
"source": {
"index": “index",
"size": 5000
},
"dest": {
"index": “index_bak",
}
}
16.索引添加别名
curl -u elastic:'password' -XPOST 'http://192.168.2.131:9200/_aliases' -H 'Content-Type: application/json' -d '{
"actions" : [
{ "add" : { "index" : “index01", "alias" : “indexalias" , "is_write_index": true} }
]
}'
标签:总结,index,http,9200,192.168,cluster,Elasticsearch,常用命令,2.131
From: https://blog.51cto.com/u_13482808/8202047