Elasticssearch提供批量操作(插入,更新,删除),批量操作的API是_bulk,此功能非常强大,因为它提供了一种非常有效的机制,可以尽可能快地进行多个操作,并尽可能少地进行网络往返。
示例1
批量插入两条数据
POST /customer/doc/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
返回结果:
{
"took": 28,
"errors": false,
"items": [
{
"index": {
"_index": "customer",
"_type": "doc",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false,
"status": 200
}
}
]
}
再次通过查询id为1的数据:
{
"_index": "customer",
"_type": "doc",
"_id": "1",
"_version": 4,
"found": true,
"_source": {
"name": "John Doe"
}
}
发现已经修改成功。
示例2
更新id为1的数据并且删除id为2的数据。
POST /customer/doc/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
返回结果:
{
"took": 18,
"errors": false,
"items": [
{
"update": {
"_index": "customer",
"_type": "doc",
"_id": "1",
"_version": 5,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200
}
}
]
}
没有错误,执行成功。看一下id为1的数据和id为2的数据。
{
"_index": "customer",
"_type": "doc",
"_id": "1",
"_version": 5,
"found": true,
"_source": {
"name": "John Doe becomes Jane Doe"
}
}
{
"_index": "customer",
"_type": "doc",
"_id": "2",
"found": false
}
一个已经更新,一个已经删除,结果符合预期。
PS:在批量操作中,一项操作的失败并不会导致整个批量操作的失败,再批量操作执行结束之后,可以看到哪些操作执行失败,哪些成功,方便我们调试。
首先我们更新一下记录,使id为1的记录变为:
{
"name": "Tony li",
"age": 10
}
然后我们构造语句,让名字为Tony li的用户的年龄加10.
POST customer/doc/_update_by_query
{
"script": {
"source": "ctx._source.age+=10",
"lang": "painless"
},
"query": {
"term": {
"name": "Tony li"
}
}
}
可以看到记录的年龄已经如预期的那样加10了。
{
"_index": "customer",
"_type": "doc",
"_id": "1",
"_version": 10,
"found": true,
"_source": {
"name": "Tony li",
"age": 20
}
}
###通过查询批量删除记录
此方法和上面批量更新差不多,只不过不用加操作,只需要构造查询条件即可。
POST customer/doc/_delete_by_query
{
"query": {
"term": {
"name": "Tony li"
}
}
}