Elasticsearch文档获取 #
一、单文档获取 #
1.1 基本获取 #
bash
GET /products/_doc/1
响应:
json
{
"_index": "products",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"name": "iPhone 15",
"price": 999,
"brand": "Apple"
}
}
1.2 文档不存在 #
bash
GET /products/_doc/999
响应:
json
{
"_index": "products",
"_id": "999",
"found": false
}
1.3 获取参数 #
| 参数 | 说明 | 默认值 |
|---|---|---|
| stored_fields | 返回存储字段 | - |
| _source | 是否返回源文档 | true |
| _source_includes | 包含字段 | - |
| _source_excludes | 排除字段 | - |
| routing | 路由值 | - |
| preference | 执行偏好 | - |
| refresh | 是否刷新 | false |
| realtime | 是否实时 | true |
| version | 版本号 | - |
| version_type | 版本类型 | - |
1.4 使用routing获取 #
bash
GET /products/_doc/1?routing=apple
1.5 指定preference #
bash
GET /products/_doc/1?preference=_local
GET /products/_doc/1?preference=_primary
| preference值 | 说明 |
|---|---|
| _local | 优先本地节点 |
| _primary | 只在主分片执行 |
| _replica | 只在副本分片执行 |
| _only_nodes:xxx | 只在指定节点执行 |
二、字段过滤 #
2.1 禁用_source #
bash
GET /products/_doc/1?_source=false
响应:
json
{
"_index": "products",
"_id": "1",
"_version": 1,
"found": true
}
2.2 包含指定字段 #
bash
GET /products/_doc/1?_source_includes=name,price
响应:
json
{
"_index": "products",
"_id": "1",
"found": true,
"_source": {
"name": "iPhone 15",
"price": 999
}
}
2.3 排除指定字段 #
bash
GET /products/_doc/1?_source_excludes=description,content
2.4 使用通配符 #
bash
GET /products/_doc/1?_source_includes=na*,pri*
2.5 组合使用 #
bash
GET /products/_doc/1?_source_includes=name,price&_source_excludes=price
三、获取存储字段 #
3.1 映射配置 #
bash
PUT /products
{
"mappings": {
"properties": {
"name": {
"type": "text",
"store": true
},
"price": {
"type": "float",
"store": true
},
"description": {
"type": "text"
}
}
}
}
3.2 获取存储字段 #
bash
GET /products/_doc/1?stored_fields=name,price
响应:
json
{
"_index": "products",
"_id": "1",
"found": true,
"fields": {
"name": ["iPhone 15"],
"price": [999.0]
}
}
四、只获取_source #
4.1 基本用法 #
bash
GET /products/_source/1
响应:
json
{
"name": "iPhone 15",
"price": 999,
"brand": "Apple"
}
4.2 字段过滤 #
bash
GET /products/_source/1?_source_includes=name,price
4.3 文档不存在 #
bash
GET /products/_source/999
返回404状态码。
五、文档存在检查 #
5.1 使用HEAD #
bash
HEAD /products/_doc/1
响应:
- 存在:200 OK
- 不存在:404 Not Found
5.2 检查source存在 #
bash
HEAD /products/_source/1
六、多文档获取(mget) #
6.1 基本用法 #
bash
GET /_mget
{
"docs": [
{ "_index": "products", "_id": "1" },
{ "_index": "products", "_id": "2" },
{ "_index": "orders", "_id": "1" }
]
}
响应:
json
{
"docs": [
{
"_index": "products",
"_id": "1",
"found": true,
"_source": { "name": "iPhone 15" }
},
{
"_index": "products",
"_id": "2",
"found": true,
"_source": { "name": "MacBook Pro" }
},
{
"_index": "orders",
"_id": "1",
"found": true,
"_source": { "total": 1998 }
}
]
}
6.2 指定索引 #
bash
GET /products/_mget
{
"docs": [
{ "_id": "1" },
{ "_id": "2" }
]
}
6.3 使用ids数组 #
bash
GET /products/_mget
{
"ids": ["1", "2", "3"]
}
6.4 字段过滤 #
bash
GET /products/_mget
{
"ids": ["1", "2"],
"_source": ["name", "price"]
}
或:
bash
GET /products/_mget
{
"docs": [
{ "_id": "1", "_source": ["name"] },
{ "_id": "2", "_source": ["name", "price"] }
]
}
6.5 指定存储字段 #
bash
GET /products/_mget
{
"docs": [
{
"_id": "1",
"stored_fields": ["name", "price"]
}
]
}
6.6 使用routing #
bash
GET /products/_mget
{
"docs": [
{ "_id": "1", "routing": "apple" },
{ "_id": "2", "routing": "samsung" }
]
}
七、批量查询(msearch) #
7.1 基本用法 #
bash
GET /_msearch
{}
{"query": {"match_all": {}}}
{"index": "products"}
{"query": {"match": {"name": "iPhone"}}}
{"index": "orders"}
{"query": {"match_all": {}}}
格式说明:
- 每行一个JSON对象
- 第一行:查询参数(可为空)
- 第二行:查询体
- 交替重复
7.2 响应格式 #
json
{
"took": 5,
"responses": [
{
"hits": {
"total": { "value": 100 },
"hits": [...]
}
},
{
"hits": {
"total": { "value": 10 },
"hits": [...]
}
}
]
}
7.3 指定索引和类型 #
bash
GET /_msearch
{"index": "products"}
{"query": {"match_all": {}}}
{"index": "orders", "type": "_doc"}
{"query": {"match_all": {}}}
7.4 查询参数 #
bash
GET /_msearch
{"index": "products", "preference": "_local"}
{"query": {"match_all": {}}}
{"index": "orders", "search_type": "dfs_query_then_fetch"}
{"query": {"match_all": {}}}
八、获取文档版本 #
8.1 获取版本信息 #
bash
GET /products/_doc/1
响应包含版本信息:
json
{
"_version": 5,
"_seq_no": 10,
"_primary_term": 1
}
8.2 指定版本获取 #
bash
GET /products/_doc/1?version=5
九、实时获取 #
9.1 realtime参数 #
bash
GET /products/_doc/1?realtime=true
说明:
realtime=true:从translog获取最新数据(默认)realtime=false:从索引段获取
9.2 刷新后获取 #
bash
GET /products/_doc/1?refresh=true
十、获取性能优化 #
10.1 使用preference #
bash
GET /products/_doc/1?preference=_local
优势:
- 减少网络开销
- 利用本地缓存
10.2 字段过滤 #
bash
GET /products/_doc/1?_source_includes=name,price
优势:
- 减少数据传输
- 提高响应速度
10.3 批量获取 #
bash
GET /products/_mget
{
"ids": ["1", "2", "3"]
}
优势:
- 减少网络请求
- 提高效率
十一、错误处理 #
11.1 常见错误 #
| 错误 | 原因 | 解决方案 |
|---|---|---|
| 404 Not Found | 文档不存在 | 检查文档ID |
| 404 Not Found | 索引不存在 | 检查索引名 |
| 400 Bad Request | 参数错误 | 检查参数格式 |
| 500 Internal Error | 服务器错误 | 检查集群状态 |
11.2 mget错误处理 #
json
{
"docs": [
{
"_index": "products",
"_id": "1",
"found": true,
"_source": {...}
},
{
"_index": "products",
"_id": "999",
"found": false
}
]
}
部分文档不存在不影响其他文档返回。
十二、最佳实践 #
12.1 获取策略 #
text
获取策略
├── 单文档获取
│ ├── 使用GET API
│ └── 指定必要字段
├── 多文档获取
│ ├── 使用mget
│ └── 控制批量大小
└── 大批量获取
├── 使用scroll
└── 或search_after
12.2 性能建议 #
text
性能优化
├── 减少返回字段
│ └── 使用_source过滤
├── 使用批量API
│ └── mget/msearch
├── 合理使用routing
│ └── 减少分片查询
└── 利用缓存
└── preference=_local
12.3 安全建议 #
text
安全建议
├── 敏感字段排除
│ └── 使用_source_excludes
├── 权限控制
│ └── 使用安全特性
└── 审计日志
└── 记录访问日志
十三、总结 #
本章介绍了Elasticsearch文档获取:
- GET API获取单个文档
- mget批量获取多个文档
- msearch批量执行多个查询
- 字段过滤减少数据传输
- HEAD检查文档存在性
- 合理使用参数优化性能
下一步,我们将学习查询基础。
最后更新:2026-03-27