使用 Postman 操作 Elasticsearch:从零开始掌握 REST API(超详细图文教程)
作者:IT之一小佬
发布日期:2025年10月19日
阅读时间:18分钟
适合人群:后端开发、搜索工程师、DevOps、初学者
🌟 引言:为什么用 Postman 操作 Elasticsearch?
Elasticsearch 是一个基于 RESTful API 的分布式搜索引擎。虽然你可以使用 curl 命令或 Kibana 控制台,但 Postman 提供了更友好的图形化界面,支持:
- 可视化请求/响应
- 环境变量管理
- 请求集合(Collections)组织
- 自动化测试与文档生成
本教程将带你使用 Postman 完成 Elasticsearch 的核心操作:创建、查询、更新、删除索引(CRUD),并附带真实截图和可复用的 JSON 示例。
一、准备工作
1. 安装并启动 Elasticsearch
# 下载并启动(以 Docker 为例)
docker run -d \
--name elasticsearch \
-p 9200:9200 \
-p 9300:9300 \
-e "discovery.type=single-node" \
-e "xpack.security.enabled=false" \
docker.elastic.co/elasticsearch/elasticsearch:8.11.0
🔔 注意:生产环境请开启安全认证,此处为演示关闭
xpack.security.enabled=false
访问 http://localhost:9200 应看到类似响应:
{
"name" : "elasticsearch",
"cluster_name" : "docker-cluster",
"version" : {
"number" : "8.11.0",
"build_flavor" : "default",
"lucene_version" : "9.4.2"
},
"tagline" : "You Know, for Search"
}
2. 安装 Postman
- 下载地址:https://www.postman.com/downloads/
- 安装后打开,登录或使用本地模式
二、Postman 基础配置
1. 创建新请求
- 点击 “New” → “HTTP Request”
- 命名请求,如:
ES - Check Connection - 选择请求方法:
GET - 输入 URL:
http://localhost:9200
(图示:创建新请求)
2. 发送请求
点击 “Send”,右侧将显示 Elasticsearch 的版本信息。
三、索引操作实战(CRUD)
✅ 1. 创建索引(Create Index)
场景:创建一个名为 products 的商品索引
请求配置:
- Method:
PUT - URL:
http://localhost:9200/products
Headers:
| Key | Value |
|---|---|
| Content-Type | application/json |
Body (raw, JSON):
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": { "type": "text" },
"price": { "type": "float" },
"category": { "type": "keyword" },
"in_stock": { "type": "boolean" },
"created_at": { "type": "date" }
}
}
}
📌 参数说明:
number_of_shards: 主分片数(默认 1)number_of_replicas: 副本数(默认 1)text: 全文检索字段(会分词)keyword: 精确匹配字段(不分词)
(图示:创建索引请求)
成功响应:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "products"
}
✅ 2. 查看索引信息(Get Index)
请求配置:
- Method:
GET - URL:
http://localhost:9200/products
响应示例:
{
"products": {
"aliases": {},
"mappings": { ... },
"settings": { ... }
}
}
✅ 3. 查看所有索引
请求配置:
- Method:
GET - URL:
http://localhost:9200/_cat/indices?v
响应示例:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open products abc123xyz 3 1 0 0 283b 283b
💡
_cat/indices返回表格格式,便于查看状态。
✅ 4. 删除索引(Delete Index)
⚠️ 警告:此操作不可逆!
请求配置:
- Method:
DELETE - URL:
http://localhost:9200/products
成功响应:
{
"acknowledged": true
}
如果索引不存在,返回 404。
✅ 5. 批量创建文档(Bulk API)
一次性插入多条数据,效率更高。
请求配置:
- Method:
POST - URL:
http://localhost:9200/_bulk - Headers:
Content-Type: application/x-ndjson(注意是 ndjson,不是 json)
Body (raw, Text):
{"index":{"_index":"products","_id":"1"}}
{"name":"iPhone 15","price":999.99,"category":"electronics","in_stock":true,"created_at":"2025-10-19"}
{"index":{"_index":"products","_id":"2"}}
{"name":"MacBook Pro","price":2399.00,"category":"electronics","in_stock":false,"created_at":"2025-10-18"}
{"index":{"_index":"products","_id":"3"}}
{"name":"Coffee Mug","price":15.50,"category":"home","in_stock":true,"created_at":"2025-10-17"}
📌 注意:
- 每行是一个独立的 JSON 对象
- 最后一行也必须有换行
ndjson= newline-delimited JSON
成功响应:
{
"took": 32,
"errors": false,
"items": [ ... ]
}
✅ 6. 查询文档(Search)
查询所有文档
请求配置:
- Method:
POST - URL:
http://localhost:9200/products/_search
Body (JSON):
{
"query": {
"match_all": {}
}
}
条件查询:查找电子产品
{
"query": {
"term": {
"category": "electronics"
}
}
}
范围查询:价格大于 100
{
"query": {
"range": {
"price": {
"gt": 100
}
}
}
}
响应示例:
{
"hits": {
"total": { "value": 2, "relation": "eq" },
"hits": [
{
"_index": "products",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "iPhone 15",
"price": 999.99,
...
}
}
]
}
}
✅ 7. 更新文档
更新单个字段
请求配置:
- Method:
POST - URL:
http://localhost:9200/products/_update/1
Body:
{
"doc": {
"price": 899.99
}
}
脚本更新(Scripted Update)
将价格打 9 折:
{
"script": {
"source": "ctx._source.price *= 0.9"
}
}
✅ 8. 删除文档
请求配置:
- Method:
DELETE - URL:
http://localhost:9200/products/_doc/1
响应:
{
"_index": "products",
"_id": "1",
"_version": 2,
"result": "deleted"
}
四、高级技巧
1. 使用环境变量(Environments)
避免硬编码 localhost:9200。
步骤:
- 点击右上角 “No Environment” → “Manage Environments”
- 添加新环境
Elasticsearch Dev - 设置变量:
es_host=http://localhost:9200
- 在 URL 中使用:
{{es_host}}/products
(图示:环境变量配置)
2. 保存为 Collection
将所有请求归类为一个集合:
- 点击 “Save” → 选择或新建 Collection(如
Elasticsearch CRUD) - 方便团队共享和导出文档
3. 添加测试断言(Tests)
在 “Tests” 标签页添加自动化检查:
// 检查响应状态码
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// 检查是否包含 ack
pm.test("Response has acknowledged", function () {
const jsonData = pm.response.json();
pm.expect(jsonData.acknowledged).to.eql(true);
});
五、常见错误与解决方案
| 错误 | 原因 | 解决方案 |
|---|---|---|
400 Bad Request | JSON 格式错误 | 检查逗号、引号、末尾换行 |
404 Not Found | 索引或文档不存在 | 确认名称拼写 |
409 Conflict | 索引已存在 | 先删除或换名 |
Content-Type missing | 未设置头 | 添加 Content-Type: application/json |
ndjson parse failure | Bulk 格式错误 | 每行独立,最后有换行 |
六、完整操作流程图
┌─────────────┐
│ 启动 ES │
└──────┬──────┘
↓
┌─────────────┐
│ Postman 连接│ → GET http://localhost:9200
└──────┬──────┘
↓
┌──────────┴──────────┐
↓ ↓
┌─────────┐ ┌─────────────┐
│ PUT │ │ DELETE │
│ /index │ │ /index │
└─────────┘ └─────────────┘
↓ ↓
┌─────────────┐ ┌─────────────┐
│ POST │ │ GET │
│ /_bulk │ │ /_search │
└─────────────┘ └─────────────┘
七、总结:Elasticsearch + Postman 最佳实践
✅ 推荐做法:
- 使用 Collection 组织 API
- 使用 Environment 管理不同环境(dev/staging/prod)
- 用 Bulk API 批量导入数据
- 开启 测试脚本 验证响应
- 导出 API 文档 供团队查阅
❌ 避免做法:
- 在生产环境关闭安全认证
- 不设
mappings导致类型推断错误 - 忘记设置
Content-Type头 - 使用
match查询keyword字段
🔚 结语
现在,你已经掌握了使用 Postman 操作 Elasticsearch 的全套技能:
- ✅ 创建/删除索引
- ✅ 批量导入数据
- ✅ 复杂条件查询
- ✅ 环境管理与自动化测试
这些技能不仅适用于 Elasticsearch,也适用于任何 RESTful API 的调试。
立即打开 Postman,动手实践吧!
💬 评论区互动:你在使用 Elasticsearch 时遇到过哪些坑?欢迎留言交流!
4421

被折叠的 条评论
为什么被折叠?



