跳到主要内容

获取文档 get

获取单个文档

GET greeting/_doc/1

返回

{
"_index" : "greeting",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"email" : "greeting@nasuyun.com",
"message" : "hello world"
}
}

获取多个文档

GET /_mget
{
"docs" : [
{
"_index" : "greeting",
"_type" : "_doc",
"_id" : "1"
},
{
"_index" : "greeting",
"_type" : "_doc",
"_id" : "2"
}
]
}

返回

{
"docs" : [
{
"_index" : "greeting",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"email" : "greeting@nasuyun.com",
"message" : "hello world"
}
},
{
"_index" : "greeting",
"_type" : "_doc",
"_id" : "2",
"found" : false
}
]
}

Realtime

默认情况下,获取 API 是实时的,不受索引刷新率的影响(当数据对搜索可见时)。如果文档已更新但尚未刷新,则获取 API 将就地发出刷新调用以使文档可见。这也将使自上次刷新以来更改的其他文档可见。为了禁用实时 GET,可以将 realtime 参数设置为 false。

GET greeting/_doc/1?realtime=false

Source 过滤

默认情况下,get 操作返回 _source 字段的内容,除非您使用了 stored_fields 参数或者 _source 字段被禁用。您可以使用 _source 参数关闭 _source 检索

GET greeting/_doc/1?_source=false

如果你只需要完整_source中的一两个字段,你可以使用_source_includes和_source_excludes参数来包含或过滤掉你需要的部分。这对于部分检索可以节省网络开销的大型文档尤其有用。两个参数都采用逗号分隔的字段列表或通配符表达式。例子

GET greeting/_doc/1?_source_includes=*message&_source_excludes=email