文档操作

人走茶凉 提交于 2019-12-12 15:37:18
  • 文档元数据
一个文档不只有数据。它还包含了元数据(metadata)——关于文档的信息。
_index:文档存储的地方,必须是全部小写,不能以下划线开头,不能包含逗号。
_type:文档代表的对象的类,可以是大写或小写,不能包含下划线或逗号。
_id:文档的唯一标识,可以自定义 _id  ,也可以让Elasticsearch帮你自动生成
  • 索引一个文档
文档通过其 _index  、 _type  、 _id  唯一确定。
新版版中使用如下三种:
/{index}/_doc/{id} 
/{index}/_doc
/{index}/_create/{id}

# 自定义id
类型:
PUT /{index}/{type}/{id}
{
"field": "value",
...
}


PUT /website/blog/123
{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}

# 相应
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

新元素: _version 。Elasticsearch中每个文档都有版本号,每当文档变化(包括删除)都会使 _version  增加

# 自增id
请求结构发生了变化: PUT  方法—— “在这个URL中存储文档”  变成了 POST  方法—— "在这个类型下存储文档"  。
(译者注:原来是把文档存储到某个ID对应的空间,现在是把这个文档添加到某个 _type  下)。

POST /website/blog/
{
"title": "My second blog entry",
"text": "Still trying this out...",
"date": "2014/01/01"
}

#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "CLlv9W4BMI_HpWYyWHR5",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

说明:自动生成的ID有22个字符长,URL-safe, Base64-encoded string universally unique identifiers, 或者叫 UUIDs。
  • 检索文档
GET /website/blog/123?pretty

#! Deprecation: [types removal] Specifying types in document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead.
{
  "_index" : "website",
  "_type" : "blog",
  "_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"
  }
}


响应包含了现在熟悉的元数据节点,增加了 _source  字段,它包含了在创建索引时我们发送给Elasticsearch的原始文档。

在任意的查询字符串中增加 pretty  参数,会让Elasticsearch美化输出(pretty-print)JSON响应以便更加容易阅读。 _source  字段不会被美化,它的样子与输入的一致。

GET请求返回的响应内容包括 {"found": true}  。这意味着文档已经找到。如果我们请求一个不存在的文档,依旧会得到一个JSON,不过 found  值变成了 false  。

此外,HTTP响应状态码也会变成 '404 Not Found'  代替 '200 OK'  .
  • 检索文档的一部分
通常, GET 请求将返回文档的全部,存储在 _source  参数中.请求个别字段可以使用 _source  参数。多个字段可以使用逗号分隔:
GET /website/blog/123?_source=title,text

只想得到 _source  字段而不要其他的元数据:
GET /website/blog/123/_source
  • 检索文档是否存在
# HEAD  请求不会返回响应体,只有HTTP头
curl -XHEAD "http://192.168.75.21:9200/website/blog/1234"

HEAD /website/blog/123

存在返回:200 - OK
不存在返回:404 - Not Found
  • 更新整个文档
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!