ElasticSearch bulk批量增删改语法(来自学习资料 + 自己整理,第27节)
生活随笔
收集整理的這篇文章主要介紹了
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 {"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节)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 17年朗动手动高配多少钱?
- 下一篇: ES中搜索结果各属性说明介绍,以及搜索中