Claude Code 跑 /git:commit-push 自动提交,Key 用 TaoToken
2026/9/16 2:02:49
Elasticsearch REST API是Elasticsearch 全部功能的唯一入口,所有操作(索引、搜索、集群管理)。
理解 REST API = 掌握 Elasticsearch 的通用语言,与客户端语言(PHP/Java/Python)。
资源导向(Resource-Oriented)
/index/_doc/id)HTTP 方法语义化
| 方法 | 用途 | 幂等性 |
|---|---|---|
GET | 查询文档/搜索 | ✅ |
POST | 创建文档(自动生成 ID) | ❌ |
PUT | 创建/更新文档(指定 ID) | ✅ |
DELETE | 删除文档/索引 | ✅ |
JSON 作为统一数据格式
error字段)无状态(Stateless)
🔑真相:ES 是“JSON over HTTP”的极致实践。
| 端点 | 用途 | 示例 |
|---|---|---|
PUT /index/_doc/id | 创建/更新文档 | PUT /articles/_doc/1 |
GET /index/_doc/id | 获取文档 | GET /articles/_doctype/1 |
DELETE /index/_doc/id | 删除文档 | DELETE /articles/_doc/1 |
POST /index/_update/id | 部分更新 | POST /articles/_update/1 { "doc": { "views": 100 } } |
| 端点 | 用途 | 示例 |
|---|---|---|
GET /index/_search | 全文搜索 | GET /articles/_search?q=title:php |
POST /index/_search | 复杂查询 | POST /articles/_search { "query": { "match": { "title": "php" } } } |
GET /index/_count | 计数 | GET /articles/_count?q=status:published |
| 端点 | 用途 | 示例 |
|---|---|---|
PUT /index | 创建索引 | PUT /articles { "settings": { "number_of_shards": 1 } } |
GET /index | 获取索引信息 | GET /articles |
DELETE /index | 删除索引 | DELETE /articles |
POST /index/_close | 关闭索引 | POST /articles/_close |
| 端点 | 用途 | 示例 |
|---|---|---|
GET /_cluster/health | 集群健康 | GET /_cluster/health?pretty |
GET /_cat/indices | 索引列表 | GET /_cat/indices?v |
GET /_nodes/stats | 节点统计 | GET /_nodes/stats |
| 端点 | 用途 | 示例 |
|---|---|---|
POST /_aliases | 管理别名 | POST /_aliases { "actions": [{ "add": { "index": "articles_v2", "alias": "articles" } }] } |
PUT /_index_template/name | 创建索引模板 | PUT /_index_template/articles { "index_patterns": ["articles*"], "template": { ... } } |
💡
_catAPI:为人类设计的可读格式(?v= verbose)。
POST /articles/_search Content-Type: application/json Authorization: Basic xxx { "query": { "match": { "title": "php" } }, "highlight": { "fields": { "title": {} } }, "from": 0, "size": 10 }{"took":15,// 耗时(ms)"timed_out":false,// 是否超时"_shards":{// 分片统计"total":1,"successful":1,"skipped":0,"failed":0},"hits":{// 搜索结果"total":{"value":5,"relation":"eq"},"max_score":0.2876821,"hits":[{"_index":"articles","_type":"_doc","_id":"1","_score":0.2876821,"_source":{"title":"PHP Guide"},"highlight":{"title":["<em>PHP</em> Guide"]}}]}}took:关键性能指标_shards:分片健康度hits:核心结果集# elasticsearch.ymlxpack.security.http.ssl.enabled:truecurl-H"Authorization: ApiKey xxx"http://es:9200/_search# elasticsearch.ymlhttp.max_content_length:100mb*:{"query":{"query_string":{"query":"title:*",// 危险!"allow_leading_wildcard":false// 禁止}}}{"from":10000,// 超过 index.max_result_window(默认 10000)会失败"size":10}# ?pretty 参数curl"http://localhost:9200/_cluster/health?pretty"# ?error_trace 参数curl"http://localhost:9200/invalid_index/_search?error_trace"curl-X POST"http://localhost:9200/articles/_explain/1"-H"Content-Type: application/json"-d' { "query": { "match": { "title": "php" } } }'{"profile":true,"query":{"match":{"title":"php"}}}PUT /index/_doc/id= 指定 ID(幂等)POST /index/_doc= 自动生成 ID(非幂等)_forcemerge手动释放;不要依赖客户端封装,
而要直接理解 HTTP + JSON。
真正的 ES 能力,
不在“客户端多熟”,
而在“API 多透”。
## 2025-10-08 REST API 实战 ### 1. 创建索引 - [ ] PUT /articles { "settings": { "number_of_shards": 1 } } ### 2. 索引文档 - [ ] PUT /articles/_doc/1 { "title": "PHP Guide" } ### 3. 搜索文档 - [ ] POST /articles/_search { "query": { "match": { "title": "php" } } } ### 4. 调试分析 - [ ] 用 ?pretty 格式化输出 - [ ] 用 _explain 分析匹配原因✅完成即掌握 ES 核心 API。
当你停止用“客户端方法”黑盒操作,
开始用“REST API”直接对话 ES,
Elasticsearch 就从工具,
变为数据基石。
这,才是专业工程师的搜索观。