https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html
语法
# 获取指定 id 的文档
GET <index>/_doc/<_id>
# 查看指定 id 的文档是否存在
HEAD <index>/_doc/<_id>
# 仅获取指定 id 文档的 _source 字段资源部分
GET <index>/_source/<_id>
# 仅查看指定文档的 _source 字段资源部分是否存在
HEAD <index>/_source/<_id>
示例
# 获取 id 为 0 的文档
GET twitter/_doc/0
> return:
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "0",
"_version" : 1,
"_seq_no" : 10,
"_primary_term" : 1,
"found": true,
"_source" : {
"user" : "kimchy",
"date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
# 查看 id 为 0 的文档是否存在
HEAD twitter/_doc/0
> return : 200 存在,404 不存在
# 获取 id 为 0 的文档 _source 部分
GET twitter/_source/0
> return:
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
默认情况下,get api 会返回该文档的 _source
部分,如果不需要,可以指定 _source=false
:
GET twitter/_doc/0?_source=false
可以对返回的 _source
部分进行过滤操作,_source_includes
指定包含、_source_excludes
指定排除,对于大型的 document 可以有效地节约网络开销,使用 ”,“
隔开多个参数,支持通配符表达式:
GET twitter/_source/1?_source_includes=user,message
GET twitter/_doc/0?_source_includes=*.id&_source_excludes=entities
stored_fields
用于指定需要检索的储存字段集,任何 store = false
的字段都会被忽略:
# 新增映射
PUT twitter
{
"mappings": {
"properties": {
"counter": {
"type": "integer",
"store": false
},
"tags": {
"type": "keyword",
"store": true
}
}
}
}
# 新增文档
PUT twitter/_doc/1
{
"counter" : 1,
"tags" : ["red"]
}
# 检索文档
GET twitter/_doc/1?stored_fields=tags,counter
> return:
{
"_index": "twitter",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no" : 22,
"_primary_term" : 1,
"found": true,
"fields": {
"tags": [
"red"
]
}
}