Get API

Get API #

按 ID 检索单条文档,返回完整 _source 及元数据。

请求格式 #

GET /<index>/_doc/<_id>
HEAD /<index>/_doc/<_id>

仅返回 _source(不含元数据):

GET /<index>/_source/<_id>
HEAD /<index>/_source/<_id>

路径参数 #

参数必需说明
<index>目标索引
<_id>文档 ID

查询参数 #

参数类型默认值说明
_sourcestring/booleantruetrue/false 启用或禁用 _source 返回,或传逗号分隔字段名仅返回指定字段
_source_includesstring_source 中要包含的字段(逗号分隔,支持通配符)
_source_excludesstring_source 中要排除的字段(逗号分隔,支持通配符)
stored_fieldsstring要返回的 stored 字段(逗号分隔)
routingstring自定义路由值。若文档写入时使用了自定义路由,检索时必须指定相同的值
preferencestring查询偏好。_local = 优先本地分片;_primary = 仅主分片;或自定义字符串
realtimebooleantrue实时读取。为 true 时不依赖刷新即可读到最新写入
refreshbooleanfalse读取前是否强制刷新
versionlong期望的版本号,不匹配时返回 409
version_typestringinternal版本类型

示例 #

获取完整文档 #

GET /website/_doc/123

响应:

{
  "_index":        "website",
  "_type":         "_doc",
  "_id":           "123",
  "_version":      1,
  "_seq_no":       0,
  "_primary_term": 1,
  "found":         true,
  "_source": {
    "title": "My first blog entry",
    "text":  "Just trying this out...",
    "date":  "2014/01/01"
  }
}

文档不存在时 foundfalse,HTTP 状态码 404

只返回部分字段 #

GET /website/_doc/123?_source_includes=title,date

只返回 _source #

GET /website/_source/123

直接返回 JSON 文档内容,不含 _index_version 等元数据。

检查文档是否存在 #

HEAD /website/_doc/123
  • 存在 → 200 OK
  • 不存在 → 404 Not Found

不传输 _source,适合只需判断存在性的场景。


参考导航 #

需求参见
批量获取多条文档Multi Get API
写入文档Index API
_source 字段控制_source 与字段存储