‹ 首页

elasticsearch

@chaterm · 收录于 1 周前

Elasticsearch 集群管理

适合你,如果负责 Elasticsearch 集群的日常运维与排障

/ 下载安装
elasticsearch.skill双击,或拖进 Claude 桌面版 / Cowork,即完成安装↓ .skill↓ .zip
用别的 agent?下载 .zip 解压,把文件夹放进它的技能目录
Claude Code~/.claude/skills/(项目级 .claude/skills/)
Codex CLI~/.codex/skills/
Cursor自动读取上面两处目录
其他工具见其文档的「skills」目录;两个下载是同一份文件,只是名字不同
/ 通过 npx 安装 校验哈希
npx oh-my-skill add chaterm/terminal-skills/elasticsearch
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- chaterm/terminal-skills/elasticsearch
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify chaterm/terminal-skills/elasticsearch
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
48GitHub stars
~1.8K上下文体积 · 单文件
镜像托管

怎么用

技能原文 SKILL.md作者撰写 · Apache-2.0 · 464c295

Elasticsearch 集群管理

概述

Elasticsearch 索引管理、查询 DSL、集群运维等技能。

集群管理
集群状态
# 集群健康
curl -X GET "localhost:9200/_cluster/health?pretty"

# 集群状态
curl -X GET "localhost:9200/_cluster/state?pretty"

# 集群统计
curl -X GET "localhost:9200/_cluster/stats?pretty"

# 节点信息
curl -X GET "localhost:9200/_nodes?pretty"
curl -X GET "localhost:9200/_nodes/stats?pretty"

# 分片分配
curl -X GET "localhost:9200/_cat/shards?v"
curl -X GET "localhost:9200/_cat/allocation?v"
Cat API
# 常用 cat 命令
curl -X GET "localhost:9200/_cat/health?v"
curl -X GET "localhost:9200/_cat/nodes?v"
curl -X GET "localhost:9200/_cat/indices?v"
curl -X GET "localhost:9200/_cat/shards?v"
curl -X GET "localhost:9200/_cat/segments?v"
curl -X GET "localhost:9200/_cat/count?v"
curl -X GET "localhost:9200/_cat/recovery?v"
curl -X GET "localhost:9200/_cat/thread_pool?v"
索引管理
索引操作
# 创建索引
curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "content": { "type": "text" },
      "timestamp": { "type": "date" },
      "status": { "type": "keyword" }
    }
  }
}'

# 删除索引
curl -X DELETE "localhost:9200/my_index"

# 查看索引
curl -X GET "localhost:9200/my_index?pretty"
curl -X GET "localhost:9200/my_index/_mapping?pretty"
curl -X GET "localhost:9200/my_index/_settings?pretty"

# 索引别名
curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
  "actions": [
    { "add": { "index": "my_index_v2", "alias": "my_index" } },
    { "remove": { "index": "my_index_v1", "alias": "my_index" } }
  ]
}'
索引设置
# 修改设置
curl -X PUT "localhost:9200/my_index/_settings" -H 'Content-Type: application/json' -d'
{
  "index": {
    "number_of_replicas": 2
  }
}'

# 关闭/打开索引
curl -X POST "localhost:9200/my_index/_close"
curl -X POST "localhost:9200/my_index/_open"

# 刷新索引
curl -X POST "localhost:9200/my_index/_refresh"

# 强制合并
curl -X POST "localhost:9200/my_index/_forcemerge?max_num_segments=1"
文档操作
CRUD
# 创建文档
curl -X POST "localhost:9200/my_index/_doc" -H 'Content-Type: application/json' -d'
{
  "title": "Hello World",
  "content": "This is a test document",
  "timestamp": "2024-01-15T10:00:00"
}'

# 指定 ID 创建
curl -X PUT "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d'
{
  "title": "Document 1"
}'

# 获取文档
curl -X GET "localhost:9200/my_index/_doc/1?pretty"

# 更新文档
curl -X POST "localhost:9200/my_index/_update/1" -H 'Content-Type: application/json' -d'
{
  "doc": {
    "title": "Updated Title"
  }
}'

# 删除文档
curl -X DELETE "localhost:9200/my_index/_doc/1"

# 批量操作
curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' -d'
{"index":{"_index":"my_index","_id":"1"}}
{"title":"Doc 1"}
{"index":{"_index":"my_index","_id":"2"}}
{"title":"Doc 2"}
'
查询 DSL
基础查询
# 匹配所有
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} }
}'

# 全文搜索
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "content": "search text"
    }
  }
}'

# 精确匹配
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "term": {
      "status": "published"
    }
  }
}'

# 范围查询
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "range": {
      "timestamp": {
        "gte": "2024-01-01",
        "lte": "2024-01-31"
      }
    }
  }
}'
复合查询
# Bool 查询
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "elasticsearch" } }
      ],
      "filter": [
        { "term": { "status": "published" } },
        { "range": { "timestamp": { "gte": "2024-01-01" } } }
      ],
      "should": [
        { "match": { "content": "tutorial" } }
      ],
      "must_not": [
        { "term": { "status": "draft" } }
      ]
    }
  }
}'
聚合查询
# 聚合
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "status_count": {
      "terms": { "field": "status" }
    },
    "avg_score": {
      "avg": { "field": "score" }
    },
    "date_histogram": {
      "date_histogram": {
        "field": "timestamp",
        "calendar_interval": "day"
      }
    }
  }
}'
备份与恢复
# 注册快照仓库
curl -X PUT "localhost:9200/_snapshot/my_backup" -H 'Content-Type: application/json' -d'
{
  "type": "fs",
  "settings": {
    "location": "/backup/elasticsearch"
  }
}'

# 创建快照
curl -X PUT "localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true"

# 查看快照
curl -X GET "localhost:9200/_snapshot/my_backup/_all?pretty"

# 恢复快照
curl -X POST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore" -H 'Content-Type: application/json' -d'
{
  "indices": "my_index",
  "rename_pattern": "(.+)",
  "rename_replacement": "restored_$1"
}'

# 删除快照
curl -X DELETE "localhost:9200/_snapshot/my_backup/snapshot_1"
常见场景
场景 1:重建索引
# Reindex
curl -X POST "localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
{
  "source": { "index": "old_index" },
  "dest": { "index": "new_index" }
}'

# 带查询条件
curl -X POST "localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
{
  "source": {
    "index": "old_index",
    "query": { "term": { "status": "active" } }
  },
  "dest": { "index": "new_index" }
}'
场景 2:分片迁移
# 排除节点
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
  "transient": {
    "cluster.routing.allocation.exclude._name": "node_to_remove"
  }
}'

# 取消排除
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
  "transient": {
    "cluster.routing.allocation.exclude._name": null
  }
}'
场景 3:性能优化
# 禁用刷新(批量导入时)
curl -X PUT "localhost:9200/my_index/_settings" -H 'Content-Type: application/json' -d'
{
  "index": { "refresh_interval": "-1" }
}'

# 恢复刷新
curl -X PUT "localhost:9200/my_index/_settings" -H 'Content-Type: application/json' -d'
{
  "index": { "refresh_interval": "1s" }
}'
故障排查

| 问题 | 排查方法 | |------|----------| | 集群 RED | _cluster/health, _cat/shards | | 分片未分配 | _cluster/allocation/explain | | 查询慢 | _nodes/hot_threads, Profile API | | 磁盘满 | _cat/allocation, 清理旧索引 | | 内存不足 | _nodes/stats, 调整 JVM |

# 分片未分配原因
curl -X GET "localhost:9200/_cluster/allocation/explain?pretty"

# 热点线程
curl -X GET "localhost:9200/_nodes/hot_threads"

# 任务列表
curl -X GET "localhost:9200/_tasks?detailed=true&actions=*search"
按 Apache-2.0 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。