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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

1 bulk語法

通過bulk語法,可以將crud所需的不同的操作放在一個語句里面。
先來查找一下看是否有數據:

查詢命令為如下時:

GET /test_index/test_type/1

查詢的結果是:

{"_index": "test_index","_type": "test_type","_id": "1","_version": 2,"found": true,"_source": {"test_field1": "test field1","test_field2": "test field2"} }

查詢命令為如下時:

GET /test_index/test_type/2

查詢結果是:

{"_index": "test_index","_type": "test_type","_id": "2","_version": 1,"found": true,"_source": {"test_content": "my test"} }

說明id為2的這個數據是存在的,同時id為1的那個數據也存在。

接下來模擬,先刪除,在創建等過程。使用bulk語法的特點是,當其中一個失敗了之后,不影響其它的操作,其它的操作該成功還是會成功,該失敗還是會失敗。

注意:
1、實際的時候,要將帶有#號的行去掉。
2、bulk api對json的語法,有嚴格的要求,每個json串不能換行,只能放一行,同時一個json串和一個json串之間,必須有換行。

POST /_bulk #id為2的數據將會被刪除 {"delete":{"_index":"test_index","_type":"test_type","_id":2}} #id為3的將會被創建 {"create":{"_index":"test_index","_type":"test_type","_id":3}} #表示id為3的文檔的內容 {"test_field":"test3"} #創建id為2的文檔 {"create":{"_index":"test_index","_type":"test_type","_id":2}} #表示的文檔的內容為test2 {"test_field":"test2"} #創建索引,id為4的 {"index":{"_index":"test_index","_type":"test_type","_id":4}} #這里是內容 {"test_field":"test4"} #其實,這里面已經存在id為1的值,下面的相當于是替換 {"index":{"_index":"test_index","_type":"test_type","_id":1}} 替換成的內容是下面的內容 {"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"} }

如果未遵循注意點2的要求,會報如下錯誤:

{"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 }

經過上面的bulk操作,依次執行下面的命令:

GET /test_index/test_type/2

結果是:

{"_index": "test_index","_type": "test_type","_id": "2","_version": 5,"found": true,"_source": {"test_field": "test2"} }

執行命令:

GET /test_index/test_type/3

運行結果是:

{"_index": "test_index","_type": "test_type","_id": "3","_version": 1,"found": true,"_source": {"test_field": "test3"} }

執行命令:

GET /test_index/test_type/4

查詢結果是:

{"_index": "test_index","_type": "test_type","_id": "4","_version": 4,"found": true,"_source": {"test_field": "test4"} }

執行命令:

{"_index": "test_index","_type": "test_type","_id": "1","_version": 6,"found": true,"_source": {"test_field": "replaced test1111","test_field2": "bulk test1"} }

總結,有哪些類型的操作可以執行呢?

(1)delete:刪除一個文檔,只要1個json串就可以了 (2)create:PUT /index/type/id/_create,強制創建,需要兩行,下一行表示所需的數據 (3)index:普通的put操作,可以是創建文檔,也可以是全量替換文檔 (5)update:執行的partial update操作

bulk操作中,任意一個操作失敗,是不會影響其他的操作的,但是在返回結果里,會告訴你異常日志

#2、bulk size最佳大小
bulk request會加載到內存里,如果太大的話,性能反而會下降,因此需要反復嘗試一個最佳的bulk size.
一般從1000 ~ 5000條數據開始,嘗試逐漸增加。另外,如果看大小的話,最好是在5~15MB之間。

總結

以上是生活随笔為你收集整理的ElasticSearch bulk批量增删改语法(来自学习资料 + 自己整理,第27节)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。