若是查詢100條數據,一條一條的查的話,就須要發送100條數據,若是進行批量查詢的話,只須要發送一次網絡請求。java
通常來講,在進行查詢的時候,若是一次性要查詢多條數據的話,那麼必定要用batch批量操做的api 儘量減小網絡開銷次數,可能能夠將性能提高數倍,甚至數十倍,很是很是之重要node
一條一條的查詢json
GET test_index/test_type/1
GET test_index/test_type/2
返回
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"test_field": "create id by myself"
}
}
複製代碼
mget批量查詢api
GET /_mget
{
"docs": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "1"
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2"
}
]
}
返回結果
{
"docs": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"test_field": "create id by myself"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"name": "Tom",
"age": 12,
"gender": "M"
}
}
]
}
複製代碼
若是查詢的document是一個index下的不一樣type種的話數組
GET /test_index/_mget
{
"docs" : [
{
"_type" : "test_type",
"_id" : 1
},
{
"_type" : "test_type",
"_id" : 2
}
]
}
複製代碼
若是查詢的數據都在同一個index下的同一個type下,最簡單了性能優化
GET /test_index/test_type/_mget
{
"ids": [1, 2]
}
複製代碼
每一個操做須要兩個 json 串,語法以下:網絡
{"action": {"metadata"}}
{"data"}
複製代碼
舉例,好比你如今要建立一個文檔,放bulk裏面,看起來會是這樣子的:數據結構
{"index": {"_index": "test_index", "_type", "test_type", "_id": "1"}}
{"test_field1": "test1", "test_field2": "test2"}
複製代碼
bulk api 對 json 的語法,有嚴格的要求,每一個json串不能換行,只能放一行,同時一個json串和一個json串之間,必須有一個換行jvm
單個json串裏面有換行的話,會報錯:elasticsearch
{
"error": {
"root_cause": [
{
"type": "json_e_o_f_exception",
"reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@79a526fa; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@79a526fa; line: 1, column: 3]"
}
],
"type": "json_e_o_f_exception",
"reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@79a526fa; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@79a526fa; line: 1, column: 3]"
},
"status": 500
}
複製代碼
POST /_bulk
{"delete": {"_index": "test_index", "_type": "test_type", "_id": "2"}}
{"create": {"_index": "test_index", "_type": "test_type", "_id":6}}
{"test_field": "create id 6"}
{"index": {"_index": "test_index", "_type": "test_type", "_id": 7}}
{"test_field": "put id 7"}
{"update": {"_index": "test_index", "_type": "test_type", "_id": 1}}
{"doc": {"test_field": "update id 1"}}
返回結果:
{
"took": 62,
"errors": false,
"items": [
{
"delete": {
"found": true,
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200
}
},
{
"create": {
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true,
"status": 201
}
},
{
"index": {
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true,
"status": 201
}
},
{
"update": {
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200
}
}
]
}
複製代碼
bulk操做中,任意一個操做失敗,是不會影響其餘的操做的,可是在返回結果裏,會告訴你異常日誌
上面咱們已經create了 _id 爲6的數據,咱們再create一次,確定會報錯,執行如下語句:
POST /_bulk
{"delete": {"_index": "test_index", "_type": "test_type", "_id": "2"}}
{"create": {"_index": "test_index", "_type": "test_type", "_id":6}}
{"test_field": "create id 6"}
{"index": {"_index": "test_index", "_type": "test_type", "_id": 9}}
{"test_field": "put id 9"}
{"update": {"_index": "test_index", "_type": "test_type", "_id": 1}}
{"doc": {"test_field": "update id 1"}}
返回結果:
{
"took": 10,
"errors": true,
"items": [
{
"delete": {
"found": false,
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_version": 1,
"result": "not_found",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 404
}
},
{
"create": {
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"status": 409,
"error": {
"type": "version_conflict_engine_exception",
"reason": "[test_type][6]: version conflict, document already exists (current version [1])",
"index_uuid": "rsiZYqiwSCC2XdR8N2bJow",
"shard": "2",
"index": "test_index"
}
}
},
{
"index": {
"_index": "test_index",
"_type": "test_type",
"_id": "9",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false,
"status": 200
}
},
{
"update": {
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 2,
"result": "noop",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200
}
}
]
}
複製代碼
能夠看到返回結果中 create 報錯
若是修改的是同一個index, 同一個index和同一個type,下面的語法也能夠:
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 request會加載到內存裏,若是太大的話,性能反而會降低,所以須要反覆嘗試一個最佳的bulk size。通常從1000~5000條數據開始,嘗試逐漸增長。另外,若是看大小的話,最好是在5~15MB之間。
bulk api奇特的json格式
{"action": {"meta"}}
{"data"}
{"action": {"meta"}}
{"data"}
複製代碼
爲何不是下面這種格式
[{
"action": {
},
"data": {
}
}]
複製代碼
bulk中的每一個操做均可能要轉發到不一樣的node的shard去執行
若是採用比較良好的json數組格式
容許任意的換行,整個可讀性很是棒,讀起來很爽,es拿到那種標準格式的json串之後,要按照下述流程去進行處理
耗費更多內存,更多的jvm gc開銷
咱們以前提到過bulk size最佳大小的那個問題,通常建議說在幾千條那樣,而後大小在10MB左右,因此說,可怕的事情來了。假設說如今100個bulk請求發送到了一個節點上去,而後每一個請求是10MB,100個請求,就是1000MB = 1GB,而後每一個請求的json都copy一份爲jsonarray對象,此時內存中的佔用就會翻倍,就會佔用2GB的內存,甚至還不止。由於弄成jsonarray以後,還可能會多搞一些其餘的數據結構,2GB+的內存佔用。
佔用更多的內存可能就會積壓其餘請求的內存使用量,好比說最重要的搜索請求,分析請求,等等,此時就可能會致使其餘請求的性能急速降低 另外的話,佔用內存更多,就會致使java虛擬機的垃圾回收次數更多,跟頻繁,每次要回收的垃圾對象更多,耗費的時間更多,致使es的java虛擬機中止工做線程的時間更多
如今的奇特格式
{"action": {"meta"}}
{"data"}
{"action": {"meta"}}
{"data"}
複製代碼
最大的優點在於,不須要將json數組解析爲一個JSONArray對象,造成一份大數據的拷貝,浪費內存空間,儘量地保證性能