批量查詢
批量查詢的優(yōu)點(diǎn):減少網(wǎng)絡(luò)開(kāi)銷
比如說(shuō)要查詢100條數(shù)據(jù),那么就要發(fā)送100次網(wǎng)絡(luò)請(qǐng)求,這個(gè)開(kāi)銷還是很大的
如果進(jìn)行批量查詢的話,查詢100條數(shù)據(jù),就只要發(fā)送1次網(wǎng)絡(luò)請(qǐng)求,網(wǎng)絡(luò)請(qǐng)求的性能開(kāi)銷縮減100倍
_mget
GET /_mget
{
"docs":[
{
"_index":"test_index1",
"_type":"test_type1",
"_id":1
},
{
"_index":"test_index2",
"_type":"test_type2",
"_id":2
}
]
}
如果查詢的document是一個(gè)index下的不同type的話
GET /test_index/_mget
{
"docs":[
{
"_type":"test_type1",
"_id":1
},
{
"_type":"test_type2",
"_id":2
}
]
}
如果查詢的數(shù)據(jù)是在同一個(gè)index下的同一個(gè)type
GET /test_index/test_type/_mget
{
"ids":[1,2]
}
批量增刪改
_bulk
- bulk api對(duì)json的語(yǔ)法,有嚴(yán)格的要求,每個(gè)json串不能換行,只能放一行,同時(shí)一個(gè)json串和一個(gè)json串之間,必須有一個(gè)換行
- bulk操作中,任意一個(gè)操作失敗,是不會(huì)影響其他的操作的,但是在返回結(jié)果里,會(huì)告訴你異常日志
-
bulk request會(huì)加載到內(nèi)存里,如果數(shù)據(jù)量太大的話,性能反而會(huì)下降,因此需要反復(fù)嘗試一個(gè)最佳的bulk size。一般從10005000條數(shù)據(jù)開(kāi)始,嘗試逐漸增加。另外,如果看大小的話,最好是在515MB之間
一般語(yǔ)法
{"action": {"metadata"}}
{"data"}
示例
POST /_bulk
{ "delete": { "_index": "test_index", "_type": "test_type", "_id": "3" }}
{ "create": { "_index": "test_index", "_type": "test_type", "_id": "12" }}
{ "test_field": "test12" }
{ "index": { "_index": "test_index", "_type": "test_type", "_id": "2" }}
{ "test_field": "replaced test2" }
{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3}}
{ "doc" : {"test_field2" : "bulk test1"}}
相關(guān)解釋:
- delete:刪除一個(gè)文檔,只要1個(gè)json串即可
- create:與 PUT /index/type/id/_create相等,強(qiáng)制創(chuàng)建
- index:普通的put操作,可以創(chuàng)建文檔,如果存在則是全量替換
- update:執(zhí)行partial update操作