日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ElasticSearch bulk批量增删改语法(来自学习资料 + 自己整理,第27节)

發(fā)布時(shí)間:2024/9/27 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ElasticSearch bulk批量增删改语法(来自学习资料 + 自己整理,第27节) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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 #id為2的數(shù)據(jù)將會(huì)被刪除 {"delete":{"_index":"test_index","_type":"test_type","_id":2}} #id為3的將會(huì)被創(chuàng)建 {"create":{"_index":"test_index","_type":"test_type","_id":3}} #表示id為3的文檔的內(nèi)容 {"test_field":"test3"} #創(chuàng)建id為2的文檔 {"create":{"_index":"test_index","_type":"test_type","_id":2}} #表示的文檔的內(nèi)容為test2 {"test_field":"test2"} #創(chuàng)建索引,id為4的 {"index":{"_index":"test_index","_type":"test_type","_id":4}} #這里是內(nèi)容 {"test_field":"test4"} #其實(shí),這里面已經(jīng)存在id為1的值,下面的相當(dāng)于是替換 {"index":{"_index":"test_index","_type":"test_type","_id":1}} 替換成的內(nèi)容是下面的內(nèi)容 {"test_field":"replaced test1111"} #下面表示將id為1的做更新操作 { "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} } { "doc" : {"test_field2" : "bulk test1"} }

即:總的操作命令為:

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。