search Cheatsheet

Elasticsearch 常用命令速查表

Elasticsearch 常用命令一页速查:_cluster/health 与 _cat/nodes/shards 集群健康、allocation/explain 未分配诊断、_cat/indices 与 ILM 生命周期、thread_pool/breaker/slowlog 慢查询排查、水位线与 forcemerge 运维操作。

把 Elasticsearch 排障现场最常用的集群健康、分片分配、索引管理、慢查询和线程池命令整理成一页,集群变 red 或查询变慢时按组往下查。

elasticsearcheskibanashardilm

集群状态 Cluster

  • curl localhost:9200/_cluster/health?pretty
    看集群 green/yellow/red 状态和分片数
  • curl localhost:9200/_cat/nodes?v
    查看节点角色、磁盘和负载
  • curl localhost:9200/_cat/health?v
    集群健康概览,监控常采
  • curl localhost:9200/_cluster/stats?pretty
    看索引数、文档数、分片数汇总
  • curl localhost:9200/_cat/allocation?v
    看各节点磁盘分片分配,水位线告警必看

分片与索引 Shard & Index

  • curl localhost:9200/_cat/indices?v
    列出所有索引的健康、文档数和大小
  • curl localhost:9200/_cat/shards?v&h=index,shard,prirep,state,node,unassigned.reason
    看分片分布和未分配原因
  • curl localhost:9200/_cluster/allocation/explain?pretty
    解释分片为什么未分配,RED 排查必备
  • curl -XDELETE localhost:9200/<index>
    删除索引,释放磁盘前先确认名字
  • curl localhost:9200/_ilm/explain/<index>?pretty
    查看索引 ILM 策略执行阶段

查询与慢日志 Query & Slowlog

  • curl localhost:9200/<index>/_count?pretty
    统计索引文档数
  • curl localhost:9200/_nodes/stats/thread_pool?pretty
    看线程池 queue 和 rejected,排查拒绝请求
  • curl localhost:9200/_nodes/stats/breaker?pretty
    看熔断器触发情况,排查大查询失败
  • tail -f /var/log/elasticsearch/*_index_search_slowlog.json
    跟踪慢查询日志
  • curl localhost:9200/<index>/_settings?pretty
    查看索引设置,排查副本数和分片数

运维与磁盘 Disk & Ops

  • curl localhost:9200/_cluster/settings?pretty
    看集群动态设置,如磁盘水位线
  • curl -XPUT localhost:9200/_cluster/settings -H "Content-Type:application/json" -d '{"transient":{"cluster.routing.allocation.disk.watermark.low":"85%"}}'
    临时调整磁盘水位线,慎用
  • curl -XPOST localhost:9200/_forcemerge?max_num_segments=1
    强制合并段,降低只读索引磁盘占用
  • df -h /var/lib/elasticsearch
    看 ES 数据盘使用率,触发水位线会拒绝写入
  • curl -XPOST localhost:9200/<index>/_close
    关闭索引释放资源,再 open 恢复

容易踩的坑

  • 集群变 red 先用 _cat/shards 找未分配分片,再用 allocation/explain 看原因,别急着强制分配。
  • 查询变慢先区分是 filter 没走缓存还是聚合基数过高,深分页用 search_after 替代 from+size。
  • 磁盘超 flood watermark 会拒绝写入,先清冷索引或调水位线,而不是直接加节点。