一、新增文档
一、指定文档ID:手动指定ID
- POST可以不指定ID,会自动生成ID
POST /teacher/_doc/1001
{
"name":"李四",
"age":23,
"phone":"17711117777"
}
- PUT必须指定ID
PUT /teacher/_doc/1002
{
"name":"王五",
"age":23,
"phone":"13222223333"
}
二、自动产生文档ID
POST /teacher/_doc
{
"name":"张三",
"age":22,
"phone":"133222222221"
}
三、批量新增数据
# 使用批量新增
POST _bulk
{"index":{"_index":"student_info","_id":11001}}
{"id":11001,"student_no":"202011001","name":"张三","english_name":"zhangsan","profession":"计算机科学与技术","sex":1,"age":20,"score":601,"birthday":"2000-09-30","desc":"备注"}
{"index":{"_index":"student_info","_id":11002}}
{"id":11002,"student_no":"202011002","name":"李四","english_name":"lisi","profession":"计算机科学与技术","sex":1,"age":21,"score":591,"birthday":"1999-06-15","desc":"我是李四的备注"}
{"index":{"_index":"student_info","_id":11003}}
{"id":11003,"student_no":"202011003","name":"王五","english_name":"wangwu","profession":"计算机科学与技术","sex":1,"age":22,"score":599,"birthday":"1998-07-20","desc":"我是王五的备注"}
{"index":{"_index":"student_info","_id":11004}}
{"id":11004,"student_no":"202011004","name":"赵六","english_name":"zhaoliu","profession":"软件工程","sex":1,"age":19,"score":620,"birthday":"2001-09-20","desc":"我是赵六的备注"}
{"index":{"_index":"student_info","_id":11005}}
{"id":11005,"student_no":"202011005","name":"小七七","english_name":"xiaoqiqi","profession":"软件工程","sex":1,"age":20,"score":598,"birthday":"2000-09-11","desc":"小七七"}
{"index":{"_index":"student_info","_id":11006}}
{"id":11006,"student_no":"202011006","name":"小小薇","english_name":"xiaoxiaowei","profession":"酒店管理","sex":2,"age":20,"score":621,"birthday":"2000-08-10","desc":"小小薇"}
{"index":{"_index":"student_info","_id":11007}}
{"id":11007,"student_no":"202011007","name":"小芳芳","english_name":"xiaofangfang","profession":"酒店管理","sex":2,"age":19,"score":620,"birthday":"2001-09-10","desc":"小芳芳"}
{"index":{"_index":"student_info","_id":11008}}
{"id":11008,"student_no":"202011008","name":"小小芳","english_name":"xiaoxiaofang","profession":"酒店管理","sex":2,"age":19,"score":620,"birthday":"2001-10-10","desc":"小小芳"}
二、查询文档
一、查询所有文档
GET /teacher/_search
二、根据ID查询指定文档
GET /teacher/_doc/1001
三、多ID查询
GET /teacher/_search
{
"query":{
"ids":{
"values":[1001,1002,1003]
}
}
}
四、mget单索引
POST /teacher/_mget
{
"ids":["1001","1002"]
}
五、mget跨索引
GET /_mget
{
"docs":[
{
"_index":"teacher",
"_id":"1001"
},
{
"_index":"student",
"_id":"1001"
}
]
}
三、修改文档
一、全修改
注意:全修改,请求方式PUT和POST都可以,全部字段均会被修改更新,可以新增字段,当ID未匹配上时,执行新增
POST /teacher/_doc/1002
{
"name":"王五",
"age":23,
"phone":"18988889999",
"desc":"我是新增的描述"
}
二、部分更新
注意:部分修改,请求方式POST,只修改部分字段数据,当ID未匹配上时,执行新增
- 需要doc包裹
- 不支持-不存在则创建
- 不支持PUT请求方式
POST /teacher/_update/1002
{
"doc":{
"name":"王五",
"age":23,
"phone":"14555554444",
"desc":"我是新增的描述3"
}
}
四、删除文档
一、删除指定文档
DELETE /teacher/_doc/1002