Elasticsearch常用命令总结
  IE5LYMWlmdvL 2023年11月19日 14 0

Elasticsearch常用命令总结

  1. 查看集群健康状态
- 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
}
  1. 各节点使用情况
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

Elasticsearch常用命令总结_sed


3. 查询ES的索引(按主分片大小排序)

http://192.168.2.131:9200/_cat/indices?v&s=pri.store.size:desc

Elasticsearch常用命令总结_sed_02


4. 查询ES节点分配情况

http://192.168.2.131:9200/_cat/allocation?v

Elasticsearch常用命令总结_sed_03


5. hot threads 查询,查询ES的性能到底是什么原因引起的

http://192.168.2.131:9200/_nodes/node131/hot_threads
  1. 设置索引的慢查询记录: 将大于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"
}'
  1. 强制进行 segment 合并
curl -u elastic:'password' -XPOST 'http:// 192.168.2.131:9200/contractcenter/_forcemerge?only_expunge_deletes=true'
  1. 查看线程池
http://192.168.2.131:9200/_cat/thread_pool?v
  1. 重新分配失败的分片(默认索引的尝试次数为5)
curl -u elastic:'password' -XPOST 'http://192.168.2.131:9200/_cluster/reroute?retry_failed=true'
  1. 分片分配设置
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} }
    ]
}'
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
  jnZtF7Co41Wg   2023年12月06日   12   0   0 sedlinux数据
  uvM09mQNI0hF   2023年11月19日   19   0   0 sedshell字符串
  eHipUjOuzYYH   2023年12月06日   15   0   0 sedbootstrapIPV6
  xIUntf9oR6GI   2023年11月28日   13   0   0 sedvim基础命令
  oIa1edJoFmXP   2023年11月24日   13   0   0 AppsedVue
IE5LYMWlmdvL