Get the FREE Ultimate OpenClaw Setup Guide →
npx machina-cli add skill chaterm/terminal-skills/elasticsearch --openclaw
Files (1)
SKILL.md
7.4 KB

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"

Source

git clone https://github.com/chaterm/terminal-skills/blob/main/database/elasticsearch/SKILL.mdView on GitHub

Overview

本技能覆盖 Elasticsearch 的集群运维、索引管理、文档 CRUD、以及查询 DSL 的常用操作。通过掌握集群状态查询、Cat API、索引的创建与设置,以及高效查询技巧,可以提升集群的可用性和查询性能。

How This Skill Works

该技能通过 RESTful HTTP 调用 Elasticsearch 的核心端点来获取集群信息、管理索引、执行查询和批量操作。常用端点包括 _cluster/health、_cat/*、_doc/_bulk、_search 以及 _settings/_mapping。执行结果通常以 JSON 返还,便于在脚本中自动化处理。

When to Use It

  • 需要快速了解集群健康、状态、或分片分配情况
  • 要创建、修改或查看索引、映射和设置
  • 执行全文检索、范围查询或精确匹配
  • 进行文档的增删改查和批量操作
  • 进行索引别名管理以实现无停机切换

Quick Start

  1. Step 1: 查看集群健康与节点信息,例如 curl -X GET localhost:9200/_cat/health?v
  2. Step 2: 创建索引 my_index,设置分片与简单映射,例如 进行索引创建与映射定义
  3. Step 3: 执行一个简单查询或批量导入文档,例如对 my_index/_search 进行 match_all 查询

Best Practices

  • 优先使用 Cat API 和状态端点进行日常监控
  • 为集群设置合理的分片数和副本数,并定期审查映射
  • 在生产环境中使用 TLS/身份验证,限制访问端点
  • 对批量操作使用 _bulk,确保逐步提交以防意外失败
  • 对查询要设置分页、大小和超时,以免资源耗尽

Example Use Cases

  • 创建 my_index 索引,设置 number_of_shards=3、number_of_replicas=1,并定义 title/content/timestamp/status 字段的映射
  • 使用 _cluster/health 和 _cat/nodes 查看集群健康和节点信息,以诊断性能问题
  • 通过 _bulk API 将多条文档一次性导入 my_index
  • 用别名 my_index 指向 my_index_v2,完成无停机的索引升级
  • 使用 match_all 或 match/term 进行简单全文检索或精确匹配

Frequently Asked Questions

Add this skill to your agents

Related Skills

Sponsor this space

Reach thousands of developers