Skip to main content
Documents
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

ElasticSearch 常用命令

内容覆盖:集群、节点、索引、分片、路由、性能、健康、任务、快照、ILM、模板、Pipeline。

全部采用 最常用、最高频、运维必备、业务常见场景 的命令。

🚀 ElasticSearch 常用命令大全(含集群命令)

结构如下:

  1. 集群命令(最重要)
  2. 节点命令
  3. 索引命令
  4. 分片命令
  5. 文档命令
  6. 搜索命令
  7. 监控和性能
  8. Task & Reindex
  9. 模板(index template)
  10. ILM 生命周期
  11. Snapshot & Restore
  12. Pipeline(ingest)
  13. 用户与角色(安全)

全部实战可用。

1. 集群命令(Cluster)

查看集群状态(最常用)

GET /_cluster/health

查看所有集群信息

GET /_cluster/state

查看未分配的 shard(必查)

GET /_cluster/allocation/explain

查看集群设置

GET /_cluster/settings?include_defaults=true

修改集群设置(临时)

PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.allocation.enable": "all"
  }
}

修改集群设置(持久)

PUT /_cluster/settings
{
  "persistent": {
    "indices.recovery.max_bytes_per_sec": "200mb"
  }
}

手动排除节点(用于下线节点)

PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.allocation.exclude._name": "node-1"
  }
}

2. 节点命令(Nodes)

查看所有节点

GET /_cat/nodes?v

查看某节点详细信息

GET /_nodes/node-1

查看角色、堆、线程池、磁盘等。

查看节点统计

GET /_nodes/stats

查看 JVM、OS、线程池等

GET /_nodes/hot_threads

查看节点的堆使用

GET /_nodes/stats/jvm

3. 索引命令(Index)

查看所有索引

GET /_cat/indices?v

创建索引

PUT /my-index
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  }
}

删除索引

DELETE /my-index

关闭索引

POST /my-index/_close

打开索引

POST /my-index/_open

查看索引配置

GET /my-index/_settings

修改副本数

PUT /my-index/_settings
{
  "number_of_replicas": 2
}

强制 merge

POST /my-index/_forcemerge?max_num_segments=1

4. 分片(Shard)

查看分片状态

GET /_cat/shards?v

查看某 index 的分片分布

GET /_cat/shards/my-index?v

查看 routing 分片逻辑

GET /my-index/_search_shards

手动移动分片

POST /_cluster/reroute
{
  "commands": [
    {
      "move": {
        "index": "my-index",
        "shard": 0,
        "from_node": "node-1",
        "to_node": "node-2"
      }
    }
  ]
}

5. 文档命令(Document)

插入文档

POST /my-index/_doc/1
{
  "title": "hello"
}

自动生成 ID

POST /my-index/_doc

获取文档

GET /my-index/_doc/1

更新文档(局部)

POST /my-index/_update/1
{
  "doc": { "title": "new title" }
}

删除文档

DELETE /my-index/_doc/1

6. 搜索命令(Search)

简单查询

GET /my-index/_search?q=title:hello

DSL 查询

POST /my-index/_search
{
  "query": {
    "match": { "title": "hello" }
  }
}

聚合查询(最常用)

POST /my-index/_search
{
  "size": 0,
  "aggs": {
    "by_day": {
      "date_histogram": {
        "field": "timestamp",
        "calendar_interval": "day"
      }
    }
  }
}

7. 性能调试(Monitoring)

慢查询日志

GET /_nodes/hot_threads

查看查询与写入实时指标

GET /_nodes/stats/indices

查看段信息

GET /my-index/_segments

查看执行计划(profile)

POST /my-index/_search?profile=true

8. Reindex / Tasks 管理

Reindex

POST /_reindex
{
  "source": { "index": "old" },
  "dest": { "index": "new" }
}

查看任务列表

GET /_tasks?detailed=true

终止任务

POST /_tasks/<task_id>/_cancel

9. 索引模板(Index Template)

查看模板

GET /_index_template

创建模板

PUT /_index_template/logs_template
{
  "index_patterns": ["logs-*"],
  "template": {
    "settings": {
      "number_of_shards": 1
    }
  }
}

10. ILM 生命周期管理

查看 ILM 状态

GET /_ilm/status

查看某索引 ILM

GET /my-index/_ilm/explain

创建 ILM 策略

PUT /_ilm/policy/logs_policy
{
  "policy": {
    "phases": {
      "hot": { "actions": { "rollover": { "max_size": "40GB" } } },
      "delete": { "min_age": "30d", "actions": { "delete": {} } }
    }
  }
}

11. 快照 & 备份(Snapshot)

注册 repository

PUT /_snapshot/my_repo
{
  "type": "fs",
  "settings": { "location": "/data/backups" }
}

创建快照

PUT /_snapshot/my_repo/snap-01

恢复快照

POST /_snapshot/my_repo/snap-01/_restore

12. Pipeline(Ingest)

查看 pipeline

GET /_ingest/pipeline

创建 pipeline

PUT /_ingest/pipeline/my_pipe
{
  "processors": [
    { "lowercase": { "field": "title" } }
  ]
}

13. 用户与角色(X-Pack Security)

创建角色

POST /_security/role/log_reader
{
  "indices": [
    {
      "names": ["logs-*"],
      "privileges": ["read"]
    }
  ]
}

创建用户

POST /_security/user/martin
{
  "password": "123456",
  "roles": ["log_reader"]
}