查询id是1、2的数据
# 批量查询
GET _mget
{
"docs":[
{
"_index":"indexname",
"_id":1
},
{
"_index":"indexname",
"_id":2
}
]
}
# 也可以把索引提到上面
GET indexname/_mget
{
"docs":[
{
"_id":1
},
{
"_id":2
}
]
}
# 也可以通过ids直接查询id
GET indexname/_mget
{
"ids":[1,2]
}
# 查询时,指定source
GET indexname/_mget
{
"docs":[
{
"_id":1,
"_source":[
"name"
]
},
{
"_id":2,
"_source":{
"include":[],
"exclude":[]
}
}
]
}
create:不存在则创建,存在则报错
delete:删除文档
update:全量替换或部分更新
index:索引(动词)
# 自动生成id
POST indexname/_doc
{
"a":"3",
"b":5
}
并没有真正的删除,只是标记为删除
如果数据不存在,创建:PUT {indexname}/_create/{id}
如果数据存在,全量替换:PUT {indexname}/_doc/{id}
PUT indexname/_doc/1?op_type=index
{
"a":1,
"b":2
}
请求后面加这个参数,只输出错误信息
PUT indexname/_doc/10?filter_path=items.*.error
{
"a":1,
"b":2
}
POST _bulk
{"create":{"_index":"product","_id":10}}
{"name":"xxxx"}
POST _bulk
{"delete":{"_index":"product","_id":10}}
部分修改,数据不存在会报错
POST _bulk
{"update":{"_index":"product","_id":10}}
{"doc":{"name":"xxxx22"}}
也可以把增删改放在一起,批量操作
POST _bulk
{"create":{"_index":"product","_id":11}}
{"name":"xxxx"}
{"create":{"_index":"product","_id":12}}
{"name":"xxxx12"}
{"update":{"_index":"product","_id":12}}
{"doc":{"name":"xxxx13"}}
{"delete":{"_index":"product","_id":12}}
{"delete":{"_index":"product","_id":11}}
{"delete":{"_index":"product","_id":10}}
如果操作数据过多,结果也会有很多,只想看错误结果:
POST _bulk?filter_path=items.*.error
{"create":{"_index":"product","_id":11}}
{"name":"xxxx"}
{"create":{"_index":"product","_id":12}}
{"name":"xxxx12"}
{"update":{"_index":"product","_id":12}}
{"doc":{"name":"xxxx13"}}
{"update":{"_index":"product","_id":13}}
{"doc":{"name":"xxxx13"}}
{"delete":{"_index":"product","_id":12}}
{"delete":{"_index":"product","_id":11}}
{"delete":{"_index":"product","_id":10}}
id为13的数据操作失败
优点:不消耗额外的内存。单条操作时,需要把{}中的数据在内存中序列化成json对象,消耗堆内存空间
缺点:人进行阅读时,可读性较差