新增文档用put并指定id,为防止覆盖已存在的文档,可以通过_create加以限制
或 用post,不指定id,会自动创建id
put方式操作 新增文档
PUT /nba/_doc/1
{
"name": "哈登",
"team_name": "⽕火箭",
"position": "得分后卫",
"play_year": "10",
"jerse_no": "13"
}
或
PUT /nba/_doc/1/_create
{
"name": "哈登",
"team_name": "⽕火箭",
"position": "得分后卫",
"play_year": "10",
"jerse_no": "13"
}
//post方式操作 新增文档
post /nba/_doc/
{
"name": "库⾥里里",
"team_name": "勇⼠士",
"position": "组织后卫",
"play_year": "10",
"jerse_no": "30"
}
更新文档
PUT nba/_doc/1
{
"name": "哈登--1",
"team_name": "⽕火箭--1",
"position": "得分后卫--1",
"play_year": "10--1"
}
更新文档的字段
通过脚本更新制定字段,其中ctx是脚本语言中的一个执行对象,先获取_source,再修改name字段
//更新字段
POST nba/_doc/1/_update
{
"script": {
"source": "ctx._source.name=\"姓名-a\""
}
}
//根据参数值,更新文档的指定的字段
POST nba/_doc/1/_update
{
"script": {
"source": "ctx._source.jerse_no+=params.age",
"params": {
"age": 4
}
}
}
添加字段author
POST nba/_doc/1/_update
{
"script": {
"source": "ctx._source.author=\"别名\""
}
}
删除字段author
POST nba/_doc/1/_update
{
"script": {
"source": "ctx._source.remove(\"author\")"
}
}
删除文档
DELETE nba/_doc/1
通过文档id获取指定的文档
GET nba/_doc/1
获得多个文档
//方式1
get /_mget
{
"docs": [
{
"_index": "nba",
"_type": "_doc",
"_id": 2
},
{
"_index": "nba",
"_type": "_doc",
"_id": 1
}
]
}
//方式2
get /nba/_mget
{
"docs": [
{
"_type": "_doc",
"_id": 2
},
{
"_type": "_doc",
"_id": 1
}
]
}
//方式3
get /nba/_doc/_mget
{
"docs": [
{
"_id": 2
},
{
"_id": 1
}
]
}
//方式4
get /nba/_doc/_mget
{
"ids":["1","2"]
}
判定文档是否存在
HEAD nba/_doc/1
新增文档,响应体说明
{
"_index" : "nba", //文档所在的索引名
"_type" : "_doc", //文档所在的类型名
"_id" : "1", //文档ID
"_version" : 1, //文档的版本
"result" : "created", //created已经创建
"_shards" : { //_shards表示索引操作的复制过程的信息。
"total" : 2, //指示应在其上执行索引操作的分片副本(主分片和副本分片)的数量。
"successful" : 1, //表示索引操作成功的分片副本数。
"failed" : 0 //在副本分片上索引操作失败的情况下包含复制相关错误。
},
"_seq_no" : 0,
"_primary_term" : 2
}
返回文档,响应体说明
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "1",
"_version" : 1, //版本号,修改后版本号会递增
"found" : true, //表明查询到该文档
"_source" : { //文档的内容
"name" : "哈登ss",
"team_name" : "⽕火箭",
"position" : "得分后卫",
"play_year" : "10",
"jerse_no" : "13"
}
}