GeekIt

Elasticsearch命令速查

集群巡检、索引映射、文档 CRUD、查询 DSL、聚合、分词、慢日志、快照恢复等 172 条命令

共 172 条命令 · 12 类场景 · 点击右侧图标可复制

命令 / 请求说明复制
curl -s -u elastic:密码 "https://10.0.0.5:9200/?pretty"查看集群名、节点名、ES 与 Lucene 版本,确认连通性
curl -s "http://10.0.0.5:9200/_cluster/health?pretty"集群健康:status 为 green/yellow/red,重点看 relocating 与 initializing 分片数
curl -s "http://10.0.0.5:9200/_cluster/health?level=indices&pretty"按索引展开健康状态,快速定位 red/yellow 索引
curl -s "http://10.0.0.5:9200/_cat/health?v"单行健康摘要,适合脚本巡检与监控采集
curl -s "http://10.0.0.5:9200/_cat/nodes?v&h=name,ip,heap.percent,ram.percent,cpu,load_1m,master,node.role"节点资源总览:堆使用率、CPU、负载与角色
curl -s "http://10.0.0.5:9200/_cat/indices?v&s=store.size:desc"索引列表按存储大小降序,找出占用最大的索引
curl -s "http://10.0.0.5:9200/_cat/shards?v&h=index,shard,prirep,state,docs,store,node&s=store:desc"分片分布与大小,UNASSIGNED 即未分配需要排查
curl -s "http://10.0.0.5:9200/_cat/allocation?v"各节点分片数、磁盘占用与剩余空间
curl -s "http://10.0.0.5:9200/_cat/master?v"查看当前主节点,排查选主异常时先确认这里
curl -s "http://10.0.0.5:9200/_cluster/stats?human&pretty"集群级统计:文档数、存储量、JVM 堆与分片总数
curl -s "http://10.0.0.5:9200/_nodes/stats/jvm,os,fs?pretty"节点 JVM 堆、GC、磁盘水位明细
curl -s "http://10.0.0.5:9200/_cat/pending_tasks?v"主节点待处理任务,持续堆积说明元数据操作过频
curl -s "http://10.0.0.5:9200/_cat/plugins?v"查看已安装插件(IK 分词、analysis-icu 等)
curl -s "http://10.0.0.5:9200/_cat/aliases?v"查看索引别名与路由配置
curl -s "http://10.0.0.5:9200/_cat/templates?v&s=order:desc"查看索引模板与组件模板优先级
curl -s "http://10.0.0.5:9200/_cluster/state/master_node,metadata?pretty"查看集群状态元数据,排查索引与模板异常
命令 / 请求说明复制
curl -X PUT "http://10.0.0.5:9200/app_logs" -H "Content-Type: application/json" -d "{ \"settings\": { \"number_of_shards\": 3, \"number_of_replicas\": 1 }, \"mappings\": { \"properties\": { \"msg\": { \"type\": \"text\" }, \"ts\": { \"type\": \"date\" } } } }"创建索引并指定分片、副本与字段映射
curl -X PUT "http://10.0.0.5:9200/app_logs/_mapping" -H "Content-Type: application/json" -d "{ \"properties\": { \"client_ip\": { \"type\": \"ip\" } } }"新增字段映射,已有字段类型不可修改,只能重建索引
curl -s "http://10.0.0.5:9200/app_logs/_mapping?pretty"查看索引映射,确认字段是 text 还是 keyword
curl -s "http://10.0.0.5:9200/app_logs/_settings?pretty"查看索引设置:分片数、副本数、refresh_interval
curl -X PUT "http://10.0.0.5:9200/app_logs/_settings" -H "Content-Type: application/json" -d "{ \"index\": { \"number_of_replicas\": 0 } }"临时取消副本加速写入与恢复,完成后务必改回
curl -X PUT "http://10.0.0.5:9200/app_logs/_settings" -H "Content-Type: application/json" -d "{ \"index\": { \"refresh_interval\": \"30s\" } }"拉长 refresh 间隔提升写入吞吐,代价是可见延迟变大
curl -X POST "http://10.0.0.5:9200/app_logs/_close"关闭索引后可修改分片数等静态设置,期间不可读写
curl -X POST "http://10.0.0.5:9200/app_logs/_open"重新打开已关闭的索引
curl -X DELETE "http://10.0.0.5:9200/app_logs"删除索引及全部数据(不可恢复,建议先快照)
curl -X POST "http://10.0.0.5:9200/_reindex?wait_for_completion=false" -H "Content-Type: application/json" -d "{ \"source\": { \"index\": \"app_logs_v1\" }, \"dest\": { \"index\": \"app_logs_v2\" } }"异步重建索引,返回 task id 供后续查询进度
curl -X POST "http://10.0.0.5:9200/_reindex" -H "Content-Type: application/json" -d "{ \"conflicts\": \"proceed\", \"source\": { \"index\": \"app_logs_v1\" }, \"dest\": { \"index\": \"app_logs_v2\" } }"遇到版本冲突继续执行,适合可容忍覆盖的场景
curl -X POST "http://10.0.0.5:9200/_reindex" -H "Content-Type: application/json" -d "{ \"source\": { \"index\": \"app_logs_v1\", \"_source\": [\"msg\", \"ts\"] }, \"dest\": { \"index\": \"app_logs_v2\", \"op_type\": \"create\" } }"只迁移指定字段并用 create 模式避免覆盖
curl -X POST "http://10.0.0.5:9200/app_logs/_rollover?dry_run=true"演练滚动到新索引,确认命名与条件是否符合预期
curl -X POST "http://10.0.0.5:9200/app_logs/_clone/app_logs_clone"克隆索引(源索引需先设为只读),用于快速复制数据
curl -X POST "http://10.0.0.5:9200/app_logs/_split/app_logs_split?copy_settings=true"拆分分片,源索引需只读且分片数按倍数增加
curl -X POST "http://10.0.0.5:9200/app_logs/_forcemerge?max_num_segments=1"合并段减少文件数(只读索引操作,写入索引禁用)
curl -s "http://10.0.0.5:9200/_cat/indices?v&health=red"只列健康状态为 red 的索引,故障排查入口
curl -X PUT "http://10.0.0.5:9200/_data_stream/logs-app"创建数据流,写入自动滚动,适合日志与指标场景
命令 / 请求说明复制
curl -X POST "http://10.0.0.5:9200/app_logs/_doc?refresh=true" -H "Content-Type: application/json" -d "{ \"msg\": \"hello\", \"ts\": \"2026-11-02T10:00:00Z\" }"写入文档,自动生成 _id,refresh=true 立即对搜索可见
curl -X PUT "http://10.0.0.5:9200/app_logs/_doc/1?refresh=wait_for" -H "Content-Type: application/json" -d "{ \"msg\": \"hello\" }"指定 _id 写入,已存在则整体覆盖
curl -X POST "http://10.0.0.5:9200/app_logs/_create/1" -H "Content-Type: application/json" -d "{ \"msg\": \"hello\" }"仅当 _id 不存在时创建,已存在返回 409
curl -X POST "http://10.0.0.5:9200/app_logs/_update/1" -H "Content-Type: application/json" -d "{ \"doc\": { \"level\": \"warn\" } }"局部更新字段,其余字段保留
curl -X POST "http://10.0.0.5:9200/app_logs/_update/1" -H "Content-Type: application/json" -d "{ \"script\": { \"source\": \"ctx._source.count += params.n\", \"params\": { \"n\": 1 } } }"脚本方式自增计数,避免读改写竞态
curl -X POST "http://10.0.0.5:9200/_bulk" -H "Content-Type: application/x-ndjson" --data-binary @bulk.ndjson批量写入,文件每两行为一组(action + doc),末尾必须换行
curl -X POST "http://10.0.0.5:9200/_bulk?filter_path=items.*.error" --data-binary @bulk.ndjson只返回失败条目,便于批量导入后核对
curl -X GET "http://10.0.0.5:9200/app_logs/_doc/1?pretty"按 _id 查询文档,包含 _source 与元数据
curl -X GET "http://10.0.0.5:9200/app_logs/_source/1?pretty"只返回 _source 内容,节省带宽
curl -X GET "http://10.0.0.5:9200/app_logs/_mget" -H "Content-Type: application/json" -d "{ \"ids\": [\"1\", \"2\", \"3\"] }"按多个 _id 批量取文档
curl -X DELETE "http://10.0.0.5:9200/app_logs/_doc/1"按 _id 删除文档,标记删除后由段合并真正回收
curl -X POST "http://10.0.0.5:9200/app_logs/_update_by_query?conflicts=proceed" -H "Content-Type: application/json" -d "{ \"query\": { \"term\": { \"level\": \"info\" } }, \"script\": { \"source\": \"ctx._source.tag = params.tag\", \"params\": { \"tag\": \"normal\" } } }"按条件批量更新(会生成大量写入,建议错峰执行)
curl -X POST "http://10.0.0.5:9200/app_logs/_delete_by_query" -H "Content-Type: application/json" -d "{ \"query\": { \"range\": { \"ts\": { \"lt\": \"2026-01-01\" } } } }"按条件删除历史数据,大批量用 wait_for_completion=false
curl -X POST "http://10.0.0.5:9200/app_logs/_count" -H "Content-Type: application/json" -d "{ \"query\": { \"match_all\": {} } }"统计满足条件的文档数,比 _search 只取 count 更轻
curl -X POST "http://10.0.0.5:9200/app_logs/_refresh"手动刷新让写入立即可搜,批量导入后调用一次
curl -X POST "http://10.0.0.5:9200/app_logs/_flush"落盘并清空 translog,重启前可减少恢复时间
curl -X PUT "http://10.0.0.5:9200/app_logs/_doc/1?if_seq_no=10&if_primary_term=2" -H "Content-Type: application/json" -d "{ \"msg\": \"updated\" }"乐观并发控制,版本不匹配返回 409,避免覆盖他人更新
curl -X POST "http://10.0.0.5:9200/app_logs/_update/1?retry_on_conflict=3" -H "Content-Type: application/json" -d "{ \"doc\": { \"level\": \"error\" } }"冲突自动重试 3 次,适合高并发自增场景
curl -X POST "http://10.0.0.5:9200/app_logs/_doc/1/_termvectors?fields=msg"查看文档分词结果,排查检索命中异常
命令 / 请求说明复制
curl -X POST "http://10.0.0.5:9200/app_logs/_search?pretty" -H "Content-Type: application/json" -d "{ \"query\": { \"match_all\": {} }, \"from\": 0, \"size\": 10 }"基础搜索:from/size 控制分页(默认最多 10000 条)
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"msg\": \"连接超时\" } } }"全文检索,text 字段会先分词再匹配,按相关度排序
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match_phrase\": { \"msg\": \"connection timeout\" } } }"短语匹配,词序相邻才命中
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"multi_match\": { \"query\": \"timeout\", \"fields\": [\"msg\", \"title^2\"] } } }"多字段检索,^2 提升字段权重
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"term\": { \"level\": \"error\" } } }"精确匹配,仅适用于 keyword 或数值字段
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"terms\": { \"level\": [\"error\", \"fatal\"] } } }"多值精确匹配,等价于 SQL 的 IN
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"range\": { \"ts\": { \"gte\": \"now-1h\", \"lte\": \"now\" } } } }"时间范围查询,支持 now-1h 等日期数学表达式
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"bool\": { \"must\": [{ \"match\": { \"msg\": \"timeout\" } }], \"filter\": [{ \"term\": { \"level\": \"error\" } }], \"must_not\": [{ \"term\": { \"env\": \"test\" } }], \"should\": [{ \"term\": { \"service\": \"api\" } }], \"minimum_should_match\": 1 } } }"组合查询:filter 不参与打分且可缓存,优先用 filter 做筛选
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"exists\": { \"field\": \"trace_id\" } } }"存在字段查询,用于排查字段缺失
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"wildcard\": { \"path\": \"/api/v1/*\" } } }"通配符查询,前缀固定时用 prefix 性能更好
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"nested\": { \"path\": \"items\", \"query\": { \"bool\": { \"must\": [{ \"term\": { \"items.sku\": \"A1\" } }] } } } } }"nested 类型查询,保证条件命中同一嵌套对象
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"_source\": [\"msg\", \"ts\"], \"query\": { \"match_all\": {} }, \"size\": 5 }"只返回指定字段,减少网络与序列化开销
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"msg\": \"timeout\" } }, \"sort\": [{ \"ts\": { \"order\": \"desc\" } }], \"track_scores\": true }"排序后仍保留相关度分数
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"msg\": \"timeout\" } }, \"highlight\": { \"fields\": { \"msg\": {} } } }"高亮命中片段,返回内容含 em 标签
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match_all\": {} }, \"collapse\": { \"field\": \"user_id\" }, \"sort\": [{ \"ts\": \"desc\" }] }"按字段去重折叠,取每个用户最近一条
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"msg\": \"timeout\" } }, \"suggest\": { \"s1\": { \"text\": \"tmeout\", \"term\": { \"field\": \"msg\" } } } }"拼写纠错与搜索建议
curl -X POST "http://10.0.0.5:9200/app_logs/_validate/query?explain=true" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"msg\": \"timeout\" } } }"校验 DSL 语法并输出改写后的查询,排查查不到数据的原因
命令 / 请求说明复制
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"aggs\": { \"by_level\": { \"terms\": { \"field\": \"level\", \"size\": 10 } } } }"按字段分组计数,size:0 不返回文档只返回聚合
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"aggs\": { \"per_hour\": { \"date_histogram\": { \"field\": \"ts\", \"calendar_interval\": \"1h\" } } } }"按小时做时间直方图统计
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"aggs\": { \"uv\": { \"cardinality\": { \"field\": \"user_id\" } } } }"基数统计近似的去重 UV,精度可通过 precision_threshold 调整
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"aggs\": { \"latency\": { \"percentiles\": { \"field\": \"cost_ms\", \"percents\": [50, 90, 99] } } } }"P50/P90/P99 分位值,比平均值更能反映长尾
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"aggs\": { \"by_level\": { \"terms\": { \"field\": \"level\" }, \"aggs\": { \"avg_cost\": { \"avg\": { \"field\": \"cost_ms\" } }, \"max_cost\": { \"max\": { \"field\": \"cost_ms\" } } } } } }"分组后嵌套指标,一次拿到各分组统计
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"query\": { \"range\": { \"ts\": { \"gte\": \"now-1d\" } } }, \"aggs\": { \"errors\": { \"filter\": { \"term\": { \"level\": \"error\" } }, \"aggs\": { \"by_service\": { \"terms\": { \"field\": \"service\" } } } } } }"filter 聚合:只统计符合条件的子集
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"aggs\": { \"cost_range\": { \"range\": { \"field\": \"cost_ms\", \"ranges\": [{ \"to\": 100 }, { \"from\": 100, \"to\": 500 }, { \"from\": 500 }] } } } }"按数值区间分桶统计,做耗时分布分析
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"aggs\": { \"paging\": { \"composite\": { \"size\": 100, \"sources\": [{ \"svc\": { \"terms\": { \"field\": \"service\" } } }, { \"lvl\": { \"terms\": { \"field\": \"level\" } } }] } } } }"composite 聚合支持 after 翻页,适合分组数超限场景
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"aggs\": { \"per_hour\": { \"date_histogram\": { \"field\": \"ts\", \"fixed_interval\": \"1h\" }, \"aggs\": { \"growth\": { \"derivative\": { \"buckets_path\": \"_count\" } } } } } }"pipeline 聚合计算环比增量
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"aggs\": { \"top_slow\": { \"terms\": { \"field\": \"service\" }, \"aggs\": { \"slowest\": { \"top_hits\": { \"size\": 1, \"sort\": [{ \"cost_ms\": \"desc\" }], \"_source\": [\"msg\", \"cost_ms\"] } } } } } }"每个分组取 Top1 原始文档,定位具体慢请求
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"aggs\": { \"by_day\": { \"date_histogram\": { \"field\": \"ts\", \"calendar_interval\": \"1d\" }, \"aggs\": { \"errors\": { \"filter\": { \"term\": { \"level\": \"error\" } } } } } } }"按天统计错误量,做稳定性趋势看板
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 0, \"aggs\": { \"by_level\": { \"terms\": { \"field\": \"level\", \"missing\": \"unknown\" } } } }"missing 参数把缺失字段归到一个桶,避免数据被漏统
命令 / 请求说明复制
curl -X POST "http://10.0.0.5:9200/_analyze" -H "Content-Type: application/json" -d "{ \"analyzer\": \"standard\", \"text\": \"Connection timeout at 10.0.0.5\" }"用内置 standard 分词器查看切词结果
curl -X POST "http://10.0.0.5:9200/_analyze" -H "Content-Type: application/json" -d "{ \"analyzer\": \"ik_max_word\", \"text\": \"网络连接超时\" }"IK 中文分词(需安装 analysis-ik 插件),最细粒度切分
curl -X POST "http://10.0.0.5:9200/_analyze" -H "Content-Type: application/json" -d "{ \"tokenizer\": \"ik_smart\", \"filter\": [\"lowercase\"], \"text\": \"北京网络故障\" }"自定义 tokenizer 与 filter 组合测试
curl -X POST "http://10.0.0.5:9200/app_logs/_analyze" -H "Content-Type: application/json" -d "{ \"field\": \"msg\", \"text\": \"连接超时\" }"按索引字段实际使用的分词器分析,验证映射是否生效
curl -X PUT "http://10.0.0.5:9200/app_logs/_mapping" -H "Content-Type: application/json" -d "{ \"properties\": { \"msg\": { \"type\": \"text\", \"analyzer\": \"ik_max_word\", \"search_analyzer\": \"ik_smart\" } } }"写入用细粒度、搜索用粗粒度,兼顾召回与精度
curl -s "http://10.0.0.5:9200/app_logs/_mapping/field/msg?pretty"查看单个字段最终生效的映射定义
curl -X PUT "http://10.0.0.5:9200/app_logs/_settings" -H "Content-Type: application/json" -d "{ \"analysis\": { \"analyzer\": { \"my_syn\": { \"tokenizer\": \"standard\", \"filter\": [\"lowercase\", \"synonym_graph\"] } } }, \"index\": { \"analysis\": { \"filter\": { \"synonym_graph\": { \"type\": \"synonym_graph\", \"synonyms\": [\"超时,timeout\"] } } } } }"配置同义词,让中文与英文术语互相命中
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"term\": { \"msg\": \"超时\" } }, \"profile\": true }"确认查询词与索引词是否一致,分词不一致是查不到的头号原因
curl -X PUT "http://10.0.0.5:9200/_ingest/pipeline/log_parse" -H "Content-Type: application/json" -d "{ \"processors\": [{ \"grok\": { \"field\": \"message\", \"patterns\": [\"%{IP:client_ip} %{WORD:method} %{NUMBER:cost_ms:int}\"] } }, { \"remove\": { \"field\": \"message\" } }] }"入库前用 grok 解析日志并抽取字段
curl -X POST "http://10.0.0.5:9200/_ingest/pipeline/log_parse/_simulate" -H "Content-Type: application/json" -d "{ \"docs\": [{ \"_source\": { \"message\": \"10.0.0.5 GET 120\" } }] }"模拟管道执行,验证解析结果再上线
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"msg\": { \"query\": \"连接超时\", \"operator\": \"and\", \"minimum_should_match\": \"80%\" } } } }"调整分词匹配逻辑,减少无关命中
curl -X PUT "http://10.0.0.5:9200/app_logs/_mapping" -H "Content-Type: application/json" -d "{ \"properties\": { \"code\": { \"type\": \"keyword\", \"ignore_above\": 256 } } }"精确匹配字段用 keyword,避免 text 分词导致的误匹配
命令 / 请求说明复制
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"from\": 9990, \"size\": 10 }"from+size 深分页会成倍消耗堆内存,超过 10000 直接报错
curl -X PUT "http://10.0.0.5:9200/app_logs/_settings" -H "Content-Type: application/json" -d "{ \"index\": { \"max_result_window\": 50000 } }"调大分页上限(不推荐,深分页请改用 search_after)
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 100, \"sort\": [{ \"ts\": \"asc\" }, { \"_id\": \"asc\" }], \"search_after\": [\"2026-11-02T10:00:00Z\", \"abc\"] }"游标翻页:用上一页最后一个排序值作为起点,无深度限制
curl -X POST "http://10.0.0.5:9200/app_logs/_search/point_in_time?keep_alive=5m"创建 PIT 快照,保证翻页期间数据一致
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 100, \"pit\": { \"id\": \"PIT_ID\", \"keep_alive\": \"5m\" }, \"sort\": [{ \"ts\": \"asc\" }] }"配合 PIT 使用 search_after 做稳定导出
curl -X DELETE "http://10.0.0.5:9200/_search/point_in_time" -H "Content-Type: application/json" -d "{ \"id\": \"PIT_ID\" }"导出结束及时释放 PIT,否则会占用资源
curl -X POST "http://10.0.0.5:9200/app_logs/_search?scroll=5m&size=1000" -H "Content-Type: application/json" -d "{ \"query\": { \"match_all\": {} }, \"sort\": [\"_doc\"] }"scroll 全量遍历,返回 _scroll_id 供后续翻页(不用于实时查询)
curl -X POST "http://10.0.0.5:9200/_search/scroll" -H "Content-Type: application/json" -d "{ \"scroll\": \"5m\", \"scroll_id\": \"SCROLL_ID\" }"继续取下一批 scroll 结果
curl -X DELETE "http://10.0.0.5:9200/_search/scroll" -H "Content-Type: application/json" -d "{ \"scroll_id\": \"SCROLL_ID\" }"主动清理 scroll 上下文,避免快照长期驻留
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"size\": 1, \"track_total_hits\": false, \"query\": { \"match_all\": {} } }"关闭精确总数统计,显著降低大索引查询开销
命令 / 请求说明复制
curl -X POST "http://10.0.0.5:9200/_aliases" -H "Content-Type: application/json" -d "{ \"actions\": [{ \"add\": { \"index\": \"app_logs_v2\", \"alias\": \"app_logs\" } }, { \"remove\": { \"index\": \"app_logs_v1\", \"alias\": \"app_logs\" } }] }"原子切换别名,业务无感知完成索引重建
curl -X POST "http://10.0.0.5:9200/_aliases" -H "Content-Type: application/json" -d "{ \"actions\": [{ \"add\": { \"index\": \"app_logs_2026_11\", \"alias\": \"app_logs_current\" } }] }"新增别名指向当月索引
curl -X PUT "http://10.0.0.5:9200/app_logs_v1/_alias/app_logs_read" -H "Content-Type: application/json" -d "{ \"filter\": { \"term\": { \"env\": \"prod\" } } }"带过滤条件的别名,同一份数据切出不同视图
curl -X POST "http://10.0.0.5:9200/app_logs/_rollover?dry_run=true" -H "Content-Type: application/json" -d "{ \"conditions\": { \"max_docs\": 10000000, \"max_age\": \"7d\", \"max_size\": \"50gb\" } }"演练滚动条件:文档数、时间或大小任一满足即滚动
curl -X PUT "http://10.0.0.5:9200/_ilm/policy/logs_policy" -H "Content-Type: application/json" -d "{ \"policy\": { \"phases\": { \"hot\": { \"actions\": { \"rollover\": { \"max_size\": \"50gb\", \"max_age\": \"7d\" } } }, \"warm\": { \"min_age\": \"7d\", \"actions\": { \"forcemerge\": { \"max_num_segments\": 1 } } }, \"delete\": { \"min_age\": \"90d\", \"actions\": { \"delete\": {} } } } } }"生命周期策略:热写、温合并、90 天自动删除
curl -X GET "http://10.0.0.5:9200/app_logs-000001/_ilm/explain?pretty"查看索引所处阶段与下一步动作,排查为何没滚动
curl -X POST "http://10.0.0.5:9200/_ilm/move/app_logs-000001" -H "Content-Type: application/json" -d "{ \"current_step\": { \"phase\": \"hot\", \"action\": \"rollover\", \"name\": \"check-rollover-ready\" } }"手动推进 ILM 步骤,卡住时应急处理
curl -X POST "http://10.0.0.5:9200/_ilm/stop"暂停 ILM 执行(维护窗口使用,之后必须 start)
curl -X POST "http://10.0.0.5:9200/_ilm/start"恢复 ILM 执行
curl -X PUT "http://10.0.0.5:9200/_index_template/app_logs_tpl" -H "Content-Type: application/json" -d "{ \"index_patterns\": [\"app_logs-*\"], \"priority\": 200, \"template\": { \"settings\": { \"number_of_shards\": 3 }, \"mappings\": { \"properties\": { \"ts\": { \"type\": \"date\" } } } } }"索引模板:匹配模式的索引创建时自动套用设置与映射
curl -X PUT "http://10.0.0.5:9200/_component_template/app_common" -H "Content-Type: application/json" -d "{ \"template\": { \"settings\": { \"number_of_replicas\": 1 } } }"组件模板便于多个索引模板复用公共配置
curl -X POST "http://10.0.0.5:9200/app_logs/_freeze"冻结低频访问索引,几乎不占堆内存,查询速度变慢
curl -X POST "http://10.0.0.5:9200/app_logs/_unfreeze"解冻索引恢复正常读写性能
命令 / 请求说明复制
curl -s "http://10.0.0.5:9200/_nodes/hot_threads"热点线程,CPU 飙升时第一手材料
curl -s "http://10.0.0.5:9200/_tasks?detailed&actions=*search*"查看正在执行的搜索任务与耗时
curl -s "http://10.0.0.5:9200/_cat/thread_pool?v&h=node_name,name,active,queue,rejected,completed"线程池队列与拒绝次数,rejected 增长说明容量不足
curl -s "http://10.0.0.5:9200/_nodes/stats/indices/search?pretty"搜索请求量、耗时与 query cache 命中情况
curl -s "http://10.0.0.5:9200/_nodes/stats/indices/query_cache,request_cache?pretty"查询缓存与请求缓存命中率,filter 查询才能命中
curl -X PUT "http://10.0.0.5:9200/app_logs/_settings" -H "Content-Type: application/json" -d "{ \"index\": { \"search\": { \"slowlog\": { \"threshold\": { \"query\": { \"warn\": \"1s\", \"info\": \"500ms\" } } } } } }"开启搜索慢日志,阈值以上写入日志便于定位慢查询
curl -X PUT "http://10.0.0.5:9200/app_logs/_settings" -H "Content-Type: application/json" -d "{ \"index\": { \"indexing\": { \"slowlog\": { \"threshold\": { \"index\": { \"warn\": \"1s\" } } } } } }"开启写入慢日志,排查 bulk 卡顿
curl -X POST "http://10.0.0.5:9200/app_logs/_search" -H "Content-Type: application/json" -d "{ \"profile\": true, \"query\": { \"match\": { \"msg\": \"timeout\" } } }"profile 输出各阶段耗时,精确定位慢在查询还是聚合
curl -X POST "http://10.0.0.5:9200/app_logs/_cache/clear?query=true"清理查询缓存(一般不需要手动清,缓存会自动失效)
curl -s "http://10.0.0.5:9200/app_logs/_segments?pretty"查看段信息,段过多会拖慢查询
curl -s "http://10.0.0.5:9200/_cat/segments/app_logs?v&s=size:desc"按大小排序查看段,判断是否需要 forcemerge
curl -s "http://10.0.0.5:9200/_cat/recovery?v&active_only=true"查看分片恢复进度,重启或扩容后关注
curl -s "http://10.0.0.5:9200/_cat/fielddata?v&s=size:desc"fielddata 堆内存大户,text 字段排序聚合的隐患
curl -s "http://10.0.0.5:9200/_cluster/allocation/explain?pretty"解释分片为何未分配,red/yellow 集群必看
curl -s "http://10.0.0.5:9200/app_logs/_stats?human&pretty"索引级统计:文档数、存储、段数、查询与写入耗时
curl -s "http://10.0.0.5:9200/_cat/nodes?v&h=name,heap.percent,heap.max,disk.avail,disk.used_percent"堆与磁盘水位,超过 85% 会触发分片迁移
命令 / 请求说明复制
curl -X PUT "http://10.0.0.5:9200/_snapshot/fs_backup" -H "Content-Type: application/json" -d "{ \"type\": \"fs\", \"settings\": { \"location\": \"/data/es_backup\", \"compress\": true } }"注册共享文件系统仓库(path.repo 需提前配置)
curl -X PUT "http://10.0.0.5:9200/_snapshot/s3_backup" -H "Content-Type: application/json" -d "{ \"type\": \"s3\", \"settings\": { \"bucket\": \"es-backup\", \"base_path\": \"prod\" } }"注册 S3 仓库,云上环境常用
curl -X POST "http://10.0.0.5:9200/_snapshot/fs_backup/_verify?pretty"校验仓库可写,注册后先验证一次
curl -s "http://10.0.0.5:9200/_snapshot/_all?pretty"查看已注册仓库与配置
curl -X PUT "http://10.0.0.5:9200/_snapshot/fs_backup/snap_20261102?wait_for_completion=false" -H "Content-Type: application/json" -d "{ \"indices\": \"app_logs,app_metrics\", \"ignore_unavailable\": true, \"include_global_state\": false }"异步创建快照,include_global_state=false 避免带走集群配置
curl -X PUT "http://10.0.0.5:9200/_snapshot/fs_backup/snap_all?wait_for_completion=true"备份全部索引并等待完成(大集群建议异步)
curl -s "http://10.0.0.5:9200/_snapshot/fs_backup/snap_20261102/_status?pretty"查看快照进度与已传分片数
curl -s "http://10.0.0.5:9200/_snapshot/fs_backup/_all?pretty"列出仓库内所有快照与时间
curl -X POST "http://10.0.0.5:9200/_snapshot/fs_backup/snap_20261102/_restore?wait_for_completion=false" -H "Content-Type: application/json" -d "{ \"indices\": \"app_logs\", \"ignore_unavailable\": true, \"include_global_state\": false }"恢复快照,目标索引已存在时会失败,需先关闭或改名
curl -X POST "http://10.0.0.5:9200/_snapshot/fs_backup/snap_20261102/_restore" -H "Content-Type: application/json" -d "{ \"indices\": \"app_logs\", \"rename_pattern\": \"app_logs\", \"rename_replacement\": \"app_logs_restore\" }"异名恢复,用于数据核对而不影响线上索引
curl -X DELETE "http://10.0.0.5:9200/_snapshot/fs_backup/snap_20261001"删除过期快照释放备份空间
curl -X POST "http://10.0.0.5:9200/_snapshot/fs_backup/_cleanup"清理仓库中的残留临时文件
curl -X PUT "http://10.0.0.5:9200/_slm/policy/daily_backup" -H "Content-Type: application/json" -d "{ \"schedule\": \"0 30 2 * * ?\", \"name\": \"<daily-{now/d}>\", \"repository\": \"fs_backup\", \"config\": { \"indices\": [\"*\"], \"include_global_state\": false }, \"retention\": { \"expire_after\": \"30d\", \"min_count\": 5, \"max_count\": 40 } }"快照生命周期策略:每天 2:30 备份并保留 30 天
命令 / 请求说明复制
curl -X POST "http://10.0.0.5:9200/_security/user/app" -H "Content-Type: application/json" -d "{ \"password\": \"密码\", \"roles\": [\"app_read\"], \"full_name\": \"业务只读账号\" }"创建业务账号并绑定角色
curl -X POST "http://10.0.0.5:9200/_security/role/app_read" -H "Content-Type: application/json" -d "{ \"cluster\": [\"monitor\"], \"indices\": [{ \"names\": [\"app_logs*\"], \"privileges\": [\"read\", \"view_index_metadata\"] }] }"自定义角色:限定索引范围与最小权限
curl -s "http://10.0.0.5:9200/_security/_authenticate?pretty"查看当前认证身份与生效角色,排查权限不生效
curl -s "http://10.0.0.5:9200/_security/user?pretty"列出全部用户(需 manage_security 权限)
curl -s "http://10.0.0.5:9200/_security/role/app_read?pretty"查看角色定义与权限明细
curl -X POST "http://10.0.0.5:9200/_security/user/app/_password" -H "Content-Type: application/json" -d "{ \"password\": \"新密码\" }"修改用户密码
curl -X POST "http://10.0.0.5:9200/_security/user/app/_disable"禁用账号,异常或离职时立即生效
curl -X POST "http://10.0.0.5:9200/_security/api_key" -H "Content-Type: application/json" -d "{ \"name\": \"logstash_write\", \"role_descriptors\": { \"writer\": { \"indices\": [{ \"names\": [\"app_logs*\"], \"privileges\": [\"create_doc\"] }] } } }"创建 API Key,供采集端使用避免明文密码
curl -X POST "http://10.0.0.5:9200/_security/role_mapping/sso_admins" -H "Content-Type: application/json" -d "{ \"roles\": [\"superuser\"], \"rules\": { \"field\": { \"realm.name\": \"saml1\" } }, \"enabled\": true }"把外部 SSO/LDAP 用户组映射到 ES 角色
xpack.security.enabled: true 与 xpack.security.transport.ssl.enabled: trueelasticsearch.yml 开启安全与节点间 TLS,8.x 默认开启
curl -s "http://10.0.0.5:9200/_license?pretty"查看许可证类型与有效期,安全特性依赖基础版以上
命令 / 请求说明复制
curl -s "http://10.0.0.5:9200/_cat/shards?v&h=index,shard,prirep,state,unassigned.reason" | findstr UNASSIGNED列出未分配分片及原因(Linux 用 grep 替换 findstr)
curl -X POST "http://10.0.0.5:9200/_cluster/reroute?dry_run=true&explain=true" -H "Content-Type: application/json" -d "{ \"commands\": [{ \"move\": { \"index\": \"app_logs\", \"shard\": 0, \"from_node\": \"node-1\", \"to_node\": \"node-2\" } }] }"演练分片迁移,确认可行后再去掉 dry_run 执行
curl -X POST "http://10.0.0.5:9200/_cluster/reroute" -H "Content-Type: application/json" -d "{ \"commands\": [{ \"allocate_stale_primary\": { \"index\": \"app_logs\", \"shard\": 0, \"node\": \"node-2\", \"accept_data_loss\": true } }] }"主分片损坏时强制分配(会丢数据,必须书面确认)
curl -X PUT "http://10.0.0.5:9200/_cluster/settings" -H "Content-Type: application/json" -d "{ \"persistent\": { \"cluster.routing.allocation.enable\": \"primaries\" } }"滚动重启前只允许主分片分配,减少恢复开销
curl -X PUT "http://10.0.0.5:9200/_cluster/settings" -H "Content-Type: application/json" -d "{ \"persistent\": { \"cluster.routing.allocation.enable\": \"all\" } }"重启完成后恢复全部分片分配
curl -X PUT "http://10.0.0.5:9200/_cluster/settings" -H "Content-Type: application/json" -d "{ \"transient\": { \"cluster.routing.allocation.exclude._name\": \"node-3\" } }"把分片迁出指定节点,用于下线与硬件维护
curl -X PUT "http://10.0.0.5:9200/_cluster/settings" -H "Content-Type: application/json" -d "{ \"persistent\": { \"cluster.routing.allocation.disk.watermark.low\": \"85%\", \"cluster.routing.allocation.disk.watermark.high\": \"90%\" } }"磁盘水位:低水位不再分配,高水位开始迁出,超过洪泛水位索引变只读
curl -X PUT "http://10.0.0.5:9200/_cluster/settings" -H "Content-Type: application/json" -d "{ \"persistent\": { \"cluster.blocks.read_only_allow_delete\": null } }"磁盘写满后解除只读锁定(先扩磁盘或清理数据)
curl -X POST "http://10.0.0.5:9200/_flush/synced"重启前刷盘,缩短分片恢复时间(7.x 后逐步被弃用)
curl -X POST "http://10.0.0.5:9200/_nodes/reload_secure_settings"热加载 keystore 中的证书与密码,无需重启节点
curl -s "http://10.0.0.5:9200/_cat/nodeattrs?v"查看节点自定义属性,用于分片感知与机架分布
curl -X PUT "http://10.0.0.5:9200/_cluster/settings" -H "Content-Type: application/json" -d "{ \"persistent\": { \"cluster.voting_config_exclusions\": [\"node-3\"] } }"主节点缩减时排除投票节点,避免脑裂风险
jvm.options: -Xms16g -Xmx16g(堆不超过物理内存 50%,且不超过 32GB)堆设置原则:过大会导致 GC 长停顿,剩余内存留给文件缓存
curl -s "http://10.0.0.5:9200/_cat/plugins?v" 与 tail -f /var/log/elasticsearch/elasticsearch.log故障排查组合:确认插件状态并实时看日志
curl -X DELETE "http://10.0.0.5:9200/app_logs-2026.09.*"清理过期索引释放磁盘(用通配符删除前务必先 _cat 确认)

运维常用组合

集群健康巡检_cluster/health?level=indices 与 _cat/nodes?v&h=name,heap.percent,disk.avail 与 _cat/shards?v
索引重建与切换_reindex?wait_for_completion=false 与 _aliases 原子切换 add/remove
慢查询定位search.slowlog.threshold.query.warn=1s 与 profile:true 与 _nodes/hot_threads
未分配分片排查_cat/shards 过滤 UNASSIGNED 与 _cluster/allocation/explain?pretty
备份与恢复_snapshot/fs_backup/snap_20261102 与 _restore 配合 rename_pattern 异名恢复
滚动重启allocation.enable=primaries 与 _flush/synced 与 重启后 allocation.enable=all

常见问题排查

1. 集群变 red

先 _cat/indices?v&health=red 定位索引,再看 _cluster/allocation/explain 找原因;主分片丢失可用快照恢复,勿轻易 accept_data_loss

2. 集群变 yellow

多为副本未分配:节点数少于副本数+1 或磁盘水位过高,补节点或临时降 number_of_replicas

3. 写入被拒绝 429

线程池 write 队列满:降低 bulk 并发、加大 refresh_interval、检查磁盘 IO 与 merge 压力

4. 查询搜不到数据

先 _analyze 确认分词是否一致,再 _validate/query?explain 看改写结果,term 查 text 字段是最常见错误

5. 深分页报 result window 错误

改变思路用 search_after + PIT 或 scroll 导出,随意调大 max_result_window 会拖垮堆内存

6. 磁盘水位告警

及时清理或 ILM 删除旧索引、forcemerge 回收段空间;写满触发只读块后需先释放空间再解除

共 172 条 Elasticsearch 命令 · 示例地址 10.0.0.5:9200 请替换为实际集群

关于Elasticsearch命令速查

在线 Elasticsearch 命令速查手册,按连接与集群概览、索引与映射管理、文档增删改查、搜索与查询 DSL、聚合与统计、分词与文本分析、分页排序与批量导出、别名模板与生命周期、性能诊断与慢日志、快照与备份恢复、安全与权限、集群运维与故障处理等 12 类场景整理 160+ 条常用 REST API 与运维命令,每条附中文说明与可复制的 curl 写法,支持关键词搜索、分类折叠和一键复制,打开网页即可查阅。涵盖 _cluster/health 与 _cat/nodes、_cat/indices 与 _cat/shards 查未分配分片、_cat/allocation 与 pending_tasks、创建索引与 number_of_shards/number_of_replicas、_mapping 与 _settings、refresh_interval 与 max_result_window、_reindex 重建索引、_aliases 原子切换、_rollover 与 ILM 生命周期、_index_template 与 component_template、数据流、_doc 与 _create 与 _update 与脚本更新、_bulk 批量写入、_update_by_query 与 _delete_by_query、match 与 match_phrase 与 multi_match 与 term 与 terms 与 range、bool 的 must/filter/must_not、nested 查询、highlight 与 collapse、search_after 与 PIT 与 scroll 深分页、terms 与 date_histogram 与 cardinality 与 percentiles 聚合、composite 与 derivative 与 top_hits、_analyze 与 ik_max_word 分词、同义词与 ingest pipeline、_nodes/hot_threads 与 _tasks 与 thread_pool、search slowlog 与 profile API、_cluster/allocation/explain 排查 red/yellow、磁盘水位与 reroute 分片迁移、快照仓库注册与 snapshot/restore、SLM 定时备份、_security/user 与 role 与 api_key、登录认证与 TLS 配置等。适用于 Elasticsearch 日常运维、集群健康巡检、慢查询与深分页优化、索引重建与别名切换、分片未分配排查、备份恢复与安全管理,以及运维面试复习。

核心能力

  • 涵盖 _cluster/health 与 _cat/nodes、_cat/indices 与 _cat/shards 查未分配分片、_cat/allocation 与 pending_tasks、创建索引与 number_of_shards/number_of_replicas、_mapping 与 _settings、refresh_interval 与 max_result_window、_reindex 重建索引、_aliases 原子切换、_rollover 与 ILM 生命周期、_index_template 与 component_template、数据流、_doc 与 _create 与 _update 与脚本更新、_bulk 批量写入、_update_by_query 与 _delete_by_query、match 与 match_phrase 与 multi_match 与 term 与 terms 与 range、bool 的 must/filter/must_not、nested 查询、highlight 与 collapse、search_after 与 PIT 与 scroll 深分页、terms 与 date_histogram 与 cardinality 与 percentiles 聚合、composite 与 derivative 与 top_hits、_analyze 与 ik_max_word 分词、同义词与 ingest pipeline、_nodes/hot_threads 与 _tasks 与 thread_pool、search slowlog 与 profile API、_cluster/allocation/explain 排查 red/yellow、磁盘水位与 reroute 分片迁移、快照仓库注册与 snapshot/restore、SLM 定时备份、_security/user 与 role 与 api_key、登录认证与 TLS 配置等

适用场景

适用于 Elasticsearch 日常运维、集群健康巡检、慢查询与深分页优化、索引重建与别名切换、分片未分配排查、备份恢复与安全管理,以及运维面试复习。Elasticsearch命令速查全程在浏览器中完成,无需安装插件,可放心用于日常办公与排障。

常见问题

Elasticsearch命令速查是免费的吗?需要注册或安装吗?
Elasticsearch命令速查完全免费,无需注册、无需下载安装任何软件,用浏览器打开网页即可直接使用,也不限制使用次数。
Elasticsearch命令速查支持哪些功能?
涵盖 _cluster/health 与 _cat/nodes、_cat/indices 与 _cat/shards 查未分配分片、_cat/allocation 与 pending_tasks、创建索引与 number_of_shards/number_of_replicas、_mapping 与 _settings、refresh_interval 与 max_result_window、_reindex 重建索引、_aliases 原子切换、_rollover 与 ILM 生命周期、_index_template 与 component_template、数据流、_doc 与 _create 与 _update 与脚本更新、_bulk 批量写入、_update_by_query 与 _delete_by_query、match 与 match_phrase 与 multi_match 与 term 与 terms 与 range、bool 的 must/filter/must_not、nested 查询、highlight 与 collapse、search_after 与 PIT 与 scroll 深分页、terms 与 date_histogram 与 cardinality 与 percentiles 聚合、composite 与 derivative 与 top_hits、_analyze 与 ik_max_word 分词、同义词与 ingest pipeline、_nodes/hot_threads 与 _tasks 与 thread_pool、search slowlog 与 profile API、_cluster/allocation/explain 排查 red/yellow、磁盘水位与 reroute 分片迁移、快照仓库注册与 snapshot/restore、SLM 定时备份、_security/user 与 role 与 api_key、登录认证与 TLS 配置等。
Elasticsearch命令速查适合哪些场景使用?
适用于 Elasticsearch 日常运维、集群健康巡检、慢查询与深分页优化、索引重建与别名切换、分片未分配排查、备份恢复与安全管理,以及运维面试复习。