PUT/POST/GET/DELETE 索引名称/_doc
操作 | 举个栗子 | 说明 |
---|---|---|
index | PUT my_index/_doc/1 {“user”:“Mike”, “comment”:“You know, for search”} |
如果ID不存在,创建新的文档,否则,先删除现有的文档,再创建新的文档,版本会增加 |
create | PUT my_index/_create/1 {“user”:“Mike”, “comment”:“You know, for search”} POST my_index/_doc (不指定ID, 自动生成) {“user”:“Mike”, “comment”:“You know, for search”} |
- 有两种方式, - 1. 指定ID - 2. 不指定ID,系统自动生成一串随机字符串 - 如果ID已经存在,会失败 |
read | GET my_index/_update/1 | 无 |
update | POST my_index/_update {“doc”: {“user”:“Mike”, “comment”:“You know, for search”}} |
文档必须已经存在,更新只会对相应字段做增量修改 |
delete | DELETE my_index/_doc/1 | 无 |
PUT users/_create/1
{
"firstName":"Jinjuan",
"lastName": "Wu",
"tags":["Java", "ElasticSearch"]
}
POST /users/_doc
{
"firstName":"Jinjuan1",
"lastName": "Wu",
"tags":["Java", "ElasticSearch"]
}
字段 | 说明 |
---|---|
_index | 索引名称 |
_type | 版本信息,同一个Id的文档,即使被删除,Version号也会不断增加 |
version | 版本信息,同一个Id的文档,即使被删除,Version号也会不断增加 |
_source | 默认包含了文档的所有原始信息 |
PUT users/_doc/1
{
"tags":["少女", "女汉子"]
}
POST users/_update/1
{
"doc":{
"tags" : [
"是少女啊",
"女汉子"
],
"emails":["[email protected]","[email protected]"]
}
}
背景:频繁地调用API查询会增加时间成本,就像多次连接数据库带来的成本一样,Bulk API能做到:
{action:{metadata}}\n
{requestbody}
action(行为) | 说明 |
---|---|
create | 文档不存在时创建 |
create | 更新文档 |
index | 创建新文档或替换已有文档 |
delete | 删除一个文档 |
POST _bulk
{"index":{"_index":"users", "_id":"1"}}
{"tags":"[email protected]"}
POST /users/_bulk
{"index":{"_id":"1"}}
{"tags":"[email protected]"}
POST /users/_bulk
{"create":{"_id":5}}
{"firstName" : "San", "lastName" : "Zhang"}
{"create":{"_id":6}}
{"firstName" : "Wu", "lastName" : "Zhao"}
POST /users/_bulk
{"delete":{"_id":5}}
{"index":{"_id":8}}
{"name":"zhangsan"}
{"delete":{"_id":6}}
GET /users/_mget
{
"ids":["1","2","3","4","5","6"]
}
{
"docs" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 16,
"_seq_no" : 19,
"_primary_term" : 1,
"found" : true,
"_source" : {
"tags" : [
"是少女啊",
"女汉子"
],
"emails" : [
"[email protected]",
"[email protected]"
],
"firstName" : "Jinjuan",
"lastName" : "Wu"
}
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "2",
"_version" : 2,
"_seq_no" : 16,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "Html5",
"price" : 45
}
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "3",
"_version" : 1,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"firstName" : "Da",
"lastName" : "Wu",
"tags" : [
"Java",
"ElasticSearch"
]
}
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "4",
"found" : false
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "5",
"_version" : 1,
"_seq_no" : 20,
"_primary_term" : 1,
"found" : true,
"_source" : {
"firstName" : "San",
"lastName" : "Zhang"
}
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "6",
"_version" : 1,
"_seq_no" : 21,
"_primary_term" : 1,
"found" : true,
"_source" : {
"firstName" : "Wu",
"lastName" : "Zhao"
}
}
]
}
问题 | 原因 |
---|---|
无法连接 | 网络故障或集群挂了 |
连接无法关闭 | 网络故障或节点出错 |
429 | 集群过于繁忙 |
4xx | 请求体格式有错 |
500 | 集群内部错误 |