Elasticsearch 常用命令速查:从集群健康到索引与查询

**摘要:**本文整理 Elasticsearch 日常运维与开发中最常用的命令,覆盖集群健康检查、索引与文档的增删改查、映射管理、搜索查询、分片与别名操作等场景,并给出可直接复制的 REST 请求示例,方便快速查阅。

1. 前置说明:命令的两种写法

Elasticsearch 对外提供 RESTful 接口,所有操作本质上都是 HTTP 请求。日常使用中通常有两种写法:

  • curl 写法:适合脚本化、服务器排查,本文示例默认使用这种形式。
  • Kibana Dev Tools 写法 :在 Kibana 的 Dev Tools 控制台中,可以省略主机地址和 curl 前缀,直接写请求行与请求体,例如 GET _cluster/health。

下文示例默认 Elasticsearch 服务地址为 http://localhost:9200,并假设已开启基础认证时需额外携带账号密码。

bash 复制代码
# 查看服务基本信息,确认版本与集群名
curl -X GET "http://localhost:9200"
若开启了安全认证,可携带用户名密码
curl -u elastic:password -X GET "http://localhost:9200"

2. 集群与节点状态命令

2.1 集群健康检查

_cluster/health 是最常用的命令,返回集群状态、节点数、分片数等关键指标。状态含义:green 表示主分片与副本分片均正常,yellow 表示主分片正常但副本未分配,red 表示存在未分配的主分片。

bash 复制代码
# 查看集群整体健康状态
curl -X GET "http://localhost:9200/_cluster/health?pretty"
查看指定索引的健康状态
curl -X GET "http://localhost:9200/_cluster/health/my_index?pretty"
查看集群健康的分片级别明细
curl -X GET "http://localhost:9200/_cluster/health?level=indices&pretty"

2.2 集群与节点信息

bash 复制代码
# 查看集群统计信息
curl -X GET "http://localhost:9200/_cluster/stats?pretty"
查看节点列表与角色
curl -X GET "http://localhost:9200/_cat/nodes?v"
查看节点详细属性
curl -X GET "http://localhost:9200/_nodes/stats?pretty"

2.3 常用 _cat 接口

_cat 系列接口以表格形式输出,适合快速巡检。?v 表示显示表头,?help 可查看可用字段。

bash 复制代码
# 查看所有索引及其状态、文档数、存储大小
curl -X GET "http://localhost:9200/_cat/indices?v"
查看分片分布情况
curl -X GET "http://localhost:9200/_cat/shards?v"
查看主分片与副本分片分配
curl -X GET "http://localhost:9200/_cat/allocation?v"
查看集群健康(表格形式)
curl -X GET "http://localhost:9200/_cat/health?v"
查看待处理任务
curl -X GET "http://localhost:9200/_cat/pending_tasks?v"

3. 索引管理命令

3.1 创建与查看索引

bash 复制代码
# 创建索引,指定分片数与副本数
curl -X PUT "http://localhost:9200/my_index" -H 'Content-Type: application/json' -d '
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  }
}'
查看索引的配置与映射
curl -X GET "http://localhost:9200/my_index?pretty"
查看索引的 settings
curl -X GET "http://localhost:9200/my_index/_settings?pretty"
判断索引是否存在
curl -I -X HEAD "http://localhost:9200/my_index"

3.2 修改索引设置

bash 复制代码
# 修改副本数(分片数创建后不可直接修改)
curl -X PUT "http://localhost:9200/my_index/_settings" -H 'Content-Type: application/json' -d '
{
  "index": {
    "number_of_replicas": 2
  }
}'
设置刷新间隔
curl -X PUT "http://localhost:9200/my_index/_settings" -H 'Content-Type: application/json' -d '
{
"index": {
"refresh_interval": "1s"
}
}'

3.3 打开、关闭与删除索引

bash 复制代码
# 关闭索引,释放资源但保留数据
curl -X POST "http://localhost:9200/my_index/_close"
打开索引
curl -X POST "http://localhost:9200/my_index/_open"
删除索引(谨慎操作,数据不可恢复)
curl -X DELETE "http://localhost:9200/my_index"
批量删除匹配前缀的索引
curl -X DELETE "http://localhost:9200/logs-2024-*"

4. 映射(Mapping)管理命令

映射用于定义字段类型与分词方式。创建索引时可直接指定映射,也可在之后追加新字段。

bash 复制代码
# 创建索引并指定映射
curl -X PUT "http://localhost:9200/blog_index" -H 'Content-Type: application/json' -d '
{
  "mappings": {
    "properties": {
      "title":    { "type": "text" },
      "author":   { "type": "keyword" },
      "views":    { "type": "integer" },
      "created":  { "type": "date" }
    }
  }
}'
查看映射
curl -X GET "http://localhost:9200/blog_index/_mapping?pretty"
为已有索引新增字段
curl -X PUT "http://localhost:9200/blog_index/_mapping" -H 'Content-Type: application/json' -d '
{
"properties": {
"tags": { "type": "keyword" }
}
}'

注意:已有字段的类型通常不能直接修改,需要重建索引并配合 _reindex 迁移数据。

5. 文档增删改查命令

5.1 写入与更新文档

bash 复制代码
# 新增文档,指定文档 ID
curl -X PUT "http://localhost:9200/blog_index/_doc/1" -H 'Content-Type: application/json' -d '
{
  "title": "Elasticsearch 入门",
  "author": "张三",
  "views": 100,
  "created": "2024-01-01"
}'
新增文档,不指定 ID,由 ES 自动生成
curl -X POST "http://localhost:9200/blog_index/_doc" -H 'Content-Type: application/json' -d '
{
"title": "Elasticsearch 常用命令",
"author": "李四",
"views": 50,
"created": "2024-02-01"
}'
局部更新文档字段
curl -X POST "http://localhost:9200/blog_index/_update/1" -H 'Content-Type: application/json' -d '
{
"doc": {
"views": 120
}
}'

5.2 查询与删除文档

bash 复制代码
# 根据 ID 查询文档
curl -X GET "http://localhost:9200/blog_index/_doc/1?pretty"
只取文档源数据
curl -X GET "http://localhost:9200/blog_index/_source/1?pretty"
删除指定文档
curl -X DELETE "http://localhost:9200/blog_index/_doc/1"
按条件删除文档
curl -X POST "http://localhost:9200/blog_index/_delete_by_query" -H 'Content-Type: application/json' -d '
{
"query": {
"term": { "author": "张三" }
}
}'

5.3 批量操作 _bulk

_bulk 用于批量写入、更新或删除,请求体每两行为一组:第一行是操作描述,第二行是文档内容(删除操作只需一行)。

bash 复制代码
curl -X POST "http://localhost:9200/blog_index/_bulk" -H 'Content-Type: application/json' -d '
{"index":{"_id":"2"}}
{"title":"ES 查询语法","author":"王五","views":30,"created":"2024-03-01"}
{"update":{"_id":"1"}}
{"doc":{"views":150}}
{"delete":{"_id":"3"}}
'

6. 搜索查询命令

6.1 基础查询

bash 复制代码
# 查询全部文档
curl -X GET "http://localhost:9200/blog_index/_search?pretty"
按字段匹配查询
curl -X GET "http://localhost:9200/blog_index/_search?pretty" -H 'Content-Type: application/json' -d '
{
"query": {
"match": { "title": "Elasticsearch" }
}
}'
精确值查询(keyword 字段)
curl -X GET "http://localhost:9200/blog_index/_search?pretty" -H 'Content-Type: application/json' -d '
{
"query": {
"term": { "author": "张三" }
}
}'

6.2 组合查询与过滤

bash 复制代码
# bool 组合查询:must + filter
curl -X GET "http://localhost:9200/blog_index/_search?pretty" -H 'Content-Type: application/json' -d '
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "Elasticsearch" } }
      ],
      "filter": [
        { "range": { "views": { "gte": 100 } } }
      ]
    }
  }
}'
范围查询
curl -X GET "http://localhost:9200/blog_index/_search?pretty" -H 'Content-Type: application/json' -d '
{
"query": {
"range": {
"created": {
"gte": "2024-01-01",
"lte": "2024-12-31"
}
}
}
}'

6.3 分页、排序与字段过滤

bash 复制代码
# 分页 + 排序 + 只返回指定字段
curl -X GET "http://localhost:9200/blog_index/_search?pretty" -H 'Content-Type: application/json' -d '
{
  "from": 0,
  "size": 10,
  "sort": [
    { "views": { "order": "desc" } }
  ],
  "_source": ["title", "author", "views"],
  "query": {
    "match_all": {}
  }
}'

6.4 聚合查询

bash 复制代码
# 按作者分组统计文档数,并计算平均浏览量
curl -X GET "http://localhost:9200/blog_index/_search?pretty" -H 'Content-Type: application/json' -d '
{
  "size": 0,
  "aggs": {
    "by_author": {
      "terms": { "field": "author" },
      "aggs": {
        "avg_views": { "avg": { "field": "views" } }
      }
    }
  }
}'

7. 分片、别名与重建索引

7.1 别名管理

bash 复制代码
# 为索引添加别名
curl -X POST "http://localhost:9200/_aliases" -H 'Content-Type: application/json' -d '
{
  "actions": [
    { "add": { "index": "blog_index", "alias": "blog" } }
  ]
}'
原子切换别名,常用于零停机重建索引
curl -X POST "http://localhost:9200/_aliases" -H 'Content-Type: application/json' -d '
{
"actions": [
{ "remove": { "index": "blog_index_v1", "alias": "blog" } },
{ "add":    { "index": "blog_index_v2", "alias": "blog" } }
]
}'
查看别名
curl -X GET "http://localhost:9200/_cat/aliases?v"

7.2 重建索引 _reindex

bash 复制代码
# 将旧索引数据迁移到新索引
curl -X POST "http://localhost:9200/_reindex" -H 'Content-Type: application/json' -d '
{
  "source": { "index": "blog_index_v1" },
  "dest":   { "index": "blog_index_v2" }
}'
查看重建任务进度
curl -X GET "http://localhost:9200/_tasks?pretty"

7.3 强制合并与刷新

bash 复制代码
# 手动刷新索引,使写入立即可搜
curl -X POST "http://localhost:9200/blog_index/_refresh"
强制合并段文件,减少段数量
curl -X POST "http://localhost:9200/blog_index/_forcemerge?max_num_segments=1"

8. 常用命令速查表

场景 命令
集群健康 GET _cluster/health
索引列表 GET _cat/indices?v
创建索引 PUT my_index
删除索引 DELETE my_index
查看映射 GET my_index/_mapping
写入文档 PUT my_index/_doc/1
查询文档 GET my_index/_doc/1
搜索 GET my_index/_search
批量操作 POST my_index/_bulk
重建索引 POST _reindex

9. 使用建议

  • 删除类命令务必确认目标 :DELETE 索引与 _delete_by_query 都不可撤销,建议先在生产环境外验证。
  • 优先使用别名访问:业务侧通过别名读写,重建索引时只需切换别名即可实现零停机。
  • 批量写入控制单批大小 :_bulk 单批建议控制在几 MB 到十几 MB,避免请求过大导致节点压力过高。
  • 善用 ?pretty 与 _cat :排查问题时先看 _cat/health、_cat/indices、_cat/shards,能快速定位异常。
相关推荐
Elasticsearch13 小时前
为什么你的 agent 会忘事:值得在其基础上继续开发的记忆系统必须完成的五项任务
elasticsearch
夜郎king18 小时前
GeoTools 实战:用 Shapefile 范围统计人口栅格数据总和(含坐标系与大数据踩坑)
大数据·geotools·java gis·地图实战
岳涛20 小时前
ES 9.2 起 ReadOnly 会设 indexing_complete,此前它要单独配
elasticsearch
天空属于哈夫克31 天前
物流信息自动化:企业微信让客户跟进更简单
大数据·自动化·企业微信
Rain的Java大神之路1 天前
别再背概念了:中台不是大数仓,湖仓一体也不是“湖+仓”——从电商下单链路一次讲透数据库、数仓、数据湖、中台、湖仓一体
大数据·数据库·后端
yyzc111111 天前
当中小微实体店面临经营困境时如何借助顺成汇创旗下荟生意品牌实现数字化智慧营销
大数据·运维·人工智能
喜欢吃燃面2 天前
Git 原理与使用(三):标签、多人协作与 Git Flow 落地
大数据·git·elasticsearch
NicOoole1232 天前
虚拟人智能配音与AI短视频制作工具赋能IP运营的即时口播内容创作平台
大数据·人工智能
拖延症晚期的Liu2 天前
数据表的操作
大数据·数据库·sql