ElasticSearch bulk批量增删改语法(来自学习资料 + 自己整理,第27节)
1 bulk語法
通過bulk語法,可以將crud所需的不同的操作放在一個(gè)語句里面。
先來查找一下看是否有數(shù)據(jù):
查詢命令為如下時(shí):
GET /test_index/test_type/1查詢的結(jié)果是:
{"_index": "test_index","_type": "test_type","_id": "1","_version": 2,"found": true,"_source": {"test_field1": "test field1","test_field2": "test field2"} }查詢命令為如下時(shí):
GET /test_index/test_type/2查詢結(jié)果是:
{"_index": "test_index","_type": "test_type","_id": "2","_version": 1,"found": true,"_source": {"test_content": "my test"} }說明id為2的這個(gè)數(shù)據(jù)是存在的,同時(shí)id為1的那個(gè)數(shù)據(jù)也存在。
接下來模擬,先刪除,在創(chuàng)建等過程。使用bulk語法的特點(diǎn)是,當(dāng)其中一個(gè)失敗了之后,不影響其它的操作,其它的操作該成功還是會(huì)成功,該失敗還是會(huì)失敗。
注意:
1、實(shí)際的時(shí)候,要將帶有#號(hào)的行去掉。
2、bulk api對(duì)json的語法,有嚴(yán)格的要求,每個(gè)json串不能換行,只能放一行,同時(shí)一個(gè)json串和一個(gè)json串之間,必須有換行。
即:總的操作命令為:
POST /_bulk {"delete":{"_index":"test_index","_type":"test_type","_id":"2"}} {"create":{"_index":"test_index","_type":"test_type","_id":"3"}} {"test_field":"test3"} {"create":{"_index":"test_index","_type":"test_type","_id":"2"}} {"test_field":"test2"} {"index":{"_index":"test_index","_type":"test_type","_id":"4"}} {"test_field":"test4"} {"index":{"_index":"test_index","_type":"test_type","_id":"1"}} {"test_field":"replaced test1111","test_field2":"test_field2"} { "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} } { "doc" : {"test_field2" : "bulk test1"} }如果未遵循注意點(diǎn)2的要求,會(huì)報(bào)如下錯(cuò)誤:
{"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@7c1cd2c1; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@7c1cd2c1; 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@7c1cd2c1; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@7c1cd2c1; line: 1, column: 3]"},"status": 500 }經(jīng)過上面的bulk操作,依次執(zhí)行下面的命令:
GET /test_index/test_type/2結(jié)果是:
{"_index": "test_index","_type": "test_type","_id": "2","_version": 5,"found": true,"_source": {"test_field": "test2"} }執(zhí)行命令:
GET /test_index/test_type/3運(yùn)行結(jié)果是:
{"_index": "test_index","_type": "test_type","_id": "3","_version": 1,"found": true,"_source": {"test_field": "test3"} }執(zhí)行命令:
GET /test_index/test_type/4查詢結(jié)果是:
{"_index": "test_index","_type": "test_type","_id": "4","_version": 4,"found": true,"_source": {"test_field": "test4"} }執(zhí)行命令:
{"_index": "test_index","_type": "test_type","_id": "1","_version": 6,"found": true,"_source": {"test_field": "replaced test1111","test_field2": "bulk test1"} }總結(jié),有哪些類型的操作可以執(zhí)行呢?
(1)delete:刪除一個(gè)文檔,只要1個(gè)json串就可以了 (2)create:PUT /index/type/id/_create,強(qiáng)制創(chuàng)建,需要兩行,下一行表示所需的數(shù)據(jù) (3)index:普通的put操作,可以是創(chuàng)建文檔,也可以是全量替換文檔 (5)update:執(zhí)行的partial update操作bulk操作中,任意一個(gè)操作失敗,是不會(huì)影響其他的操作的,但是在返回結(jié)果里,會(huì)告訴你異常日志
#2、bulk size最佳大小
bulk request會(huì)加載到內(nèi)存里,如果太大的話,性能反而會(huì)下降,因此需要反復(fù)嘗試一個(gè)最佳的bulk size.
一般從1000 ~ 5000條數(shù)據(jù)開始,嘗試逐漸增加。另外,如果看大小的話,最好是在5~15MB之間。
總結(jié)
以上是生活随笔為你收集整理的ElasticSearch bulk批量增删改语法(来自学习资料 + 自己整理,第27节)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 17年朗动手动高配多少钱?
- 下一篇: ES中搜索结果各属性说明介绍,以及搜索中