共计 2891 个字符,预计需要花费 8 分钟才能阅读完成。
[toc]
单文档 get API
1. Get 根本查问
GET weibo/_doc/1
output:
{
"_index" : "weibo",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "niewj",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
2. head 查问文档是否存在
GET weibo/_doc/2
后果:
{
"_index" : "weibo",
"_type" : "_doc",
"_id" : "2",
"found" : false
}
能够看到 id= 2 的文档不存在:
HEAD weibo/_doc/2
后果: 404 - Not Found
文档 id= 1 是存在的: GET weibo/_doc/1
后果:
{
"_index" : "weibo",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "niewj",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
用 HEAD
查问: HEAD weibo/_doc/1
, 后果: 200 - OK
HEAD: 查不到: 404; 查失去: 200;
3. get 查问索引的实时性
如果文档曾经更新,但还没有刷新,那么 get API 将收回一个刷新调用,使文档可见; 默认状况下,get API 就是实时的,不受索引刷新速率的影响.(当然也能够禁用 realtime GET,可将 realtime 参数设为 false。)
4. \ _source 的禁用
默认状况下,get 操作返回 source 内容,除非禁用_source 字段:
GET weibo/_doc/1
:
{
"_index" : "weibo",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "niewj",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
禁用: GET weibo/_doc/1?_source=false
:
{
"_index" : "weibo",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true
}
5. \_source 字段的过滤(excludes/includes)
如果您只有 source 中的一个或两个字段,能够应用 _source_include
和_source_exclude
参数来蕴含或过滤:
- _source_includes 只想查问 \_source 节点中的 user 和 message 字段:
GET weibo/_doc/1?_source_includes=user,message
{
"_index" : "weibo",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"message" : "trying out Elasticsearch",
"user" : "niewj"
}
}
- _source 上述的蕴含, 能够简写为: _source
GET weibo/_doc/1?_source=user,message
- source_excludes 想排除 \_source 节点中的 message 字段, 其余的全展现:
GET weibo/_doc/1?_source_excludes=message
{
"_index" : "weibo",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"post_date" : "2009-11-15T14:12:12",
"user" : "niewj"
}
}
6. 间接应用 \_source 获取数据
应用 /{index}/\_source/{id} 只获取文档的 \_source 字段,而不蕴含任何其余附加内容。例如:
GET weibo/_source/1
{
"user" : "niewj",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
能够看到, 只有纯数据返回; 也能够应用下面的 _source 的过滤字段:
GET weibo/_source/1?_source=user
{"user" : "niewj"}
7. 索引时的 stored_fields 参数
默认状况下,字段值被索引以使其可搜寻,但不存储它们(的原始值); 这意味着能够查到字段,但不能检索其原始字段值。举例:
# 设置 mapping 中 counter 字段只索引不存储;tags 字段索引且存储;
PUT twitter
{
"mappings": {
"properties": {
"counter": {
"type": "integer",
"store": false
},
"tags": {
"type": "keyword",
"store": true
}
}
}
}
索引一条记录:
# 索引一条文档, id=1
PUT twitter/_doc/1
{
"counter" : 1,
"tags" : ["red"]
}
查问 GET twitter/_doc/1
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"counter" : 1,
"tags" : ["red"]
}
}
都能够查到; 应用 stored_fields 参数查问 : GET twitter/_doc/1?stored_fields=tags,counter
:
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"fields" : {
"tags" : ["red"]
}
}
能够看到, 没了 counter 字段; 因为 mapping 里设置了 store: false
8. 索引查问性能相干参数之: refresh
refresh
设为true
,以在 get 之前刷新相干分片,使其可搜寻。在将其设置为 true 之前,应该认真思考并确认: 因为这可能导致系统负载过重(升高索引速度)。