📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

根据文档 ID 获取文档内容、源字段或检查文档是否存在。

API #

GET /{index}/_doc/{id}
HEAD /{index}/_doc/{id}
GET /{index}/_doc/{id}/_source
HEAD /{index}/_doc/{id}/_source
GET /{index}/{type}/_doc/{id}

API 的作用 #

该 API 用于根据文档 ID 获取文档信息。

API 变体 #

API作用返回内容
GET /{index}/_doc/{id}获取文档元数据 + 源字段
HEAD /{index}/_doc/{id}检查文档是否存在仅状态码
GET /{index}/_doc/{id}/_source获取源字段仅源字段
GET /{index}/{type}/_doc/{id}获取文档(已弃用)元数据 + 源字段

实时性 #

默认情况下,API 是实时的,即使文档未刷新也能获取到最新版本。

API 的参数 #

路由参数 #

参数类型是否必填描述
{index}字符串必需索引名称
{id}字符串必需文档 ID
{type}字符串文档类型(已弃用)

Query String 参数 #

参数类型是否必填默认值描述
preference字符串-指定从哪个分片获取结果:_local(本地)、自定义值
realtime布尔值true是否实时获取:true(实时)、false(近实时)
refresh布尔值false是否刷新分片以获取最新版本
routing字符串-路由参数,确保请求到达正确的分片
stored_fields列表-返回存储的字段(逗号分隔)
_source布尔值/字符串true是否包含 _source 字段或指定返回的字段
_source_excludes字符串-排除的源字段(逗号分隔)
_source_includes字符串-包含的源字段(逗号分隔)
version整数-指定要获取的文档版本号
version_type枚举值-版本类型:externalexternal_gte

示例 #

获取完整文档 #

GET /my_index/_doc/1

响应示例:

{
  "_index": "my_index",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "user": "kimchy",
    "post_date": "2026-02-04T08:00:00",
    "message": "Trying out Easysearch"
  }
}

仅获取源字段 #

GET /my_index/_doc/1/_source

响应示例:

{
  "user": "kimchy",
  "post_date": "2026-02-04T08:00:00",
  "message": "Trying out Easysearch"
}

检查文档是否存在 #

HEAD /my_index/_doc/1

如果文档存在,返回 200 OK;否则返回 404 Not Found

获取特定字段 #

GET /my_index/_doc/1?_source=user,message

排除大字段 #

GET /my_index/_doc/1?_source_excludes=large_field,comments

只包含特定字段 #

GET /my_index/_doc/1?_source_includes=name,email

使用路由 #

GET /my_index/_doc/1?routing=user123

从本地分片获取 #

GET /my_index/_doc/1?preference=_local

指定版本 #

GET /my_index/_doc/1?version=2&version_type=external

近实时获取 #

GET /my_index/_doc/1?realtime=false

刷新后获取 #

GET /my_index/_doc/1?refresh=true

获取存储字段 #

GET /my_index/_doc/1?stored_fields=field1,field2

组合过滤参数 #

GET /my_index/_doc/1?_source_includes=name,title&_source_excludes=metadata

响应字段说明 #

完整文档响应 #

字段描述
_index文档所在的索引
_type文档类型(固定为 _doc
_id文档 ID
_version文档版本号
_seq_no序列号
_primary_term主版本号
found是否找到文档:truefalse
_source文档的源数据
fields存储的字段(如果使用 stored_fields

文档未找到响应 #

{
  "_index": "my_index",
  "_type": "_doc",
  "_id": "1",
  "found": false
}

参数详解 #

preference #

描述
_local从本地分片获取
_primary从主分片获取
自定义值从指定分片获取

realtime #

描述
true实时获取(默认),可能需要从事务日志读取
false近实时获取,只从已刷新的段读取

_source 参数 #

格式描述
true返回所有源字段(默认)
false不返回源字段
field1,field2只返回指定字段

错误处理 #

文档不存在 #

{
  "_index": "my_index",
  "_type": "_doc",
  "_id": "999",
  "found": false
}

索引不存在 #

{
  "error": {
    "type": "index_not_found_exception",
    "reason": "no such index [my_index]"
  },
  "status": 404
}

版本不匹配 #

{
  "error": {
    "type": "version_conflict_engine_exception",
    "reason": "version conflict"
  },
  "status": 409
}

使用场景 #

场景 1:获取用户信息 #

GET /users/_doc/user123

场景 2:获取配置 #

GET /configs/_doc/app_config/_source

场景 3:检查文档存在 #

HEAD /products/_doc/PROD-001

场景 4:只获取需要的字段 #

GET /users/_doc/123?_source=name,email

场景 5:排除大字段 #

GET /articles/_doc/1?_source_excludes=content,attachments

场景 6:使用路由确保正确分片 #

GET /logs/_doc/log123?routing=shard1

性能考虑 #

参数性能影响
realtime=true可能需要从事务日志读取,较慢
realtime=false只从已刷新的段读取,较快
preference=_local避免网络开销,最快
_source=false减少数据传输,更快
排除大字段减少数据传输,更快

最佳实践 #

  1. 字段过滤:使用 _source_includes 只获取需要的字段
  2. 排除大字段:使用 _source_excludes 排除不需要的大字段
  3. 本地读取:使用 preference=_local 提高性能
  4. HEAD 请求:只需检查存在时使用 HEAD 方法
  5. 批量获取:需要获取多个文档时使用 _mget API