官方文档:
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html
1.1 添加文档
(1)指定文档ID
PUT blog/_doc/1
{
"title":"1、VMware Workstation虚拟机软件安装图解",
"author":"chengyuqiang",
"content":"1、VMware Workstation虚拟机软件安装图解...",
"url":"http://x.co/6nc81"
}
Elasticsearch服务会返回一个JSON格式的响应。
{
"_index" : "blog",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 2
}
响应结果说明:
- _index:文档所在的索引名
- _type:文档所在的类型名
- _id:文档ID
- _version:文档的版本
- result:created已经创建
- _shards: _shards表示索引操作的复制过程的信息。
- total:指示应在其上执行索引操作的分片副本(主分片和副本分片)的数量。
- successful:表示索引操作成功的分片副本数。
- failed:在副本分片上索引操作失败的情况下包含复制相关错误。
(2)不指定文档ID
添加文档时可以不指定文档id,则文档id是自动生成的字符串。注意,需要使用POST方法,而不是PUT方法。
POST blog/_doc
{
"title":"2、Linux服务器安装图解",
"author":"chengyuqiang",
"content":"2、Linux服务器安装图解解...",
"url":"http://x.co/6nc82"
}
{
"_index" : "blog",
"_type" : "_doc",
"_id" : "5P2-O2gBNSQY7o-KMw2P",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
1.2 获取文档
(1)通过文档id获取指定的文档
GET blog/_doc/1
{
"_index" : "blog",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"title" : "1、VMware Workstation虚拟机软件安装图解",
"author" : "chengyuqiang",
"content" : "1、VMware Workstation虚拟机软件安装图解...",
"url" : "http://x.co/6nc81"
}
}
响应结果说明:
- found值为true,表明查询到该文档
- _source字段是文档的内容
(2)文档不存在的情况
GET blog/_doc/2
{
"_index" : "blog",
"_type" : "_doc",
"_id" : "2",
"found" : false
}
found字段值为false表明查询的文档不存在。
(3)判定文档是否存在
HEAD blog/_doc/1
200 - OK
1.3 更新文档
更改id为1的文档,删除了author,修改content字段。
PUT blog/_doc/1
{
"title":"1、VMware Workstation虚拟机软件安装图解",
"content":"下载得到VMware-workstation-full-15.0.2-10952284.exe可执行文件...",
"url":"http://x.co/6nc81"
}
{
"_index" : "blog",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
_version更新为2
查看该文档
GET blog/_doc/1
{
"_index" : "blog",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source" : {
"title" : "1、VMware Workstation虚拟机软件安装图解",
"content" : "下载得到VMware-workstation-full-15.0.2-10952284.exe可执行文件...",
"url" : "http://x.co/6nc81"
}
}
(2)添加文档时,防止覆盖已存在的文档,可以通过_create加以限制。
PUT blog/_doc/1/_create
{
"title":"1、VMware Workstation虚拟机软件安装图解",
"content":"下载得到VMware-workstation-full-15.0.2-10952284.exe可执行文件...",
"url":"http://x.co/6nc81"
}
该文档已经存在,添加失败。
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[_doc][1]: version conflict, document already exists (current version [2])",
"index_uuid": "GqC2fSqPS06GRfTLmh1TLg",
"shard": "1",
"index": "blog"
}
],
"type": "version_conflict_engine_exception",
"reason": "[_doc][1]: version conflict, document already exists (current version [2])",
"index_uuid": "GqC2fSqPS06GRfTLmh1TLg",
"shard": "1",
"index": "blog"
},
"status": 409
}
(3)更新文档的字段
通过脚本更新制定字段,其中ctx是脚本语言中的一个执行对象,先获取_source,再修改content字段
POST blog/_doc/1/_update
{
"script": {
"source": "ctx._source.content=\"从官网下载VMware-workstation,双击可执行文件进行安装...\""
}
}