Elasticsearch技术解析与实战(七)Elasticsearch批量操作
批量查詢(xún)
1.如果查詢(xún)的document是不同index下的不同type種的話
GET /_mget {"docs" : [{"_index" : "test_index","_type" : "test_type","_id" : 1},{"_index" : "test_index","_type" : "test_type","_id" : 2}] }2.如果查詢(xún)的document是一個(gè)index下的不同type種的話
GET /test_index/_mget {"docs" : [{"_type" : "test_type","_id" : 1},{"_type" : "test_type","_id" : 2}] }3.如果查詢(xún)的數(shù)據(jù)都在同一個(gè)index下的同一個(gè)type下,最簡(jiǎn)單了
GET /test_index/test_type/_mget {"ids": [1, 2] }mget的重要性:
? 可以說(shuō)mget是很重要的,一般來(lái)說(shuō),在進(jìn)行查詢(xún)的時(shí)候,如果一次性要查詢(xún)多條數(shù)據(jù)的話,那么一定要用batch批量操作的api
? 盡可能減少網(wǎng)絡(luò)開(kāi)銷(xiāo)次數(shù),可能可以將性能提升數(shù)倍,甚至數(shù)十倍,非常非常之重要
bulk語(yǔ)法
??bulk api對(duì)json的語(yǔ)法,有嚴(yán)格的要求,每個(gè)json串不能換行,只能放一行,同時(shí)一個(gè)json串和一個(gè)json串之間,必須有一個(gè)換行
? bulk操作中,任意一個(gè)操作失敗,是不會(huì)影響其他的操作的,但是在返回結(jié)果里,會(huì)告訴你異常日志
第一種
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"} }第二種
POST /test_index/_bulk { "delete": { "_type": "test_type", "_id": "3" }} { "create": { "_type": "test_type", "_id": "12" }} { "test_field": "test12" } { "index": { "_type": "test_type" }} { "test_field": "auto-generate id test" } { "index": { "_type": "test_type", "_id": "2" }} { "test_field": "replaced test2" } { "update": { "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} } { "doc" : {"test_field2" : "bulk test1"} }第三種
POST /test_index/test_type/_bulk { "delete": { "_id": "3" }} { "create": { "_id": "12" }} { "test_field": "test12" } { "index": { }} { "test_field": "auto-generate id test" } { "index": { "_id": "2" }} { "test_field": "replaced test2" } { "update": { "_id": "1", "_retry_on_conflict" : 3} } { "doc" : {"test_field2" : "bulk test1"} }bulk size最佳大小
? bulk request會(huì)加載到內(nèi)存里,如果太大的話,性能反而會(huì)下降,因此需要反復(fù)嘗試一個(gè)最佳的bulk size。一般從1000~5000條數(shù)據(jù)開(kāi)始,嘗試逐漸增加。另外,如果看大小的話,最好是在5~15MB之間。
總結(jié)
以上是生活随笔為你收集整理的Elasticsearch技术解析与实战(七)Elasticsearch批量操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Elasticsearch技术解析与实战
- 下一篇: B树、B+树、LSM树以及其典型应用场景