1. 程式人生 > >ElasticSearch6.2.4(10)——bulk簡單操作

ElasticSearch6.2.4(10)——bulk簡單操作

1.bulk

POST /_bulk
{"delete":{"_index":"index_test2","_type":"product1","_id":"1"}}
{"create":{"_index":"index_test2","_type":"product1","_id":"1"}}
{"test_name":"name1","test_age":15}
{"index":{"_index":"index_test2","_type":"product1","_id":"2"}}
{"test_name":"name2","test_age":20}
{"update":{"_index":"index_test2","_type":"product1","_id":"1","retry_on_conflict":3}}
{"doc":{"name":"jieke"}}


{
  "took": 287,
  "errors": false,
  "items": [
    {
      "delete": {
        "_index": "index_test2",
        "_type": "product1",
        "_id": "1",
        "_version": 1,
        "result": "not_found",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 404
      }
    },
    {
      "create": {
        "_index": "index_test2",
        "_type": "product1",
        "_id": "1",
        "_version": 2,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "index_test2",
        "_type": "product1",
        "_id": "2",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "update": {
        "_index": "index_test2",
        "_type": "product1",
        "_id": "1",
        "_version": 3,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 2,
        "_primary_term": 1,
        "status": 200
      }
    }
  ]
}
有哪些型別的操作可以執行呢?
(1)delete:刪除一個文件,只要1個json串就可以了
(2)create:PUT /index/type/id/_create,強制建立
(3)index:普通的put操作,可以是建立文件,也可以是全量替換文件
(4)update:執行的partial update操作

bluk注意事項和了解
bulk api對json的語法,有嚴格的要求,每個json串不能換行,只能放一行,同時一個json串和一個json串之間,必須有一個換行
bulk操作中,任意一個操作失敗,是不會影響其他的操作的,但是在返回結果裡,會告訴你異常日誌
bulk request會載入到記憶體裡,如果太大的話,效能反而會下降,因此需要反覆嘗試一個最佳的bulk size。一般從1000~5000條資料開始,嘗試逐漸增加。另外,如果看大小的話,最好是在5~15MB之間。
bluk為什麼不用換行的json串呢?
如果使用換行的json串,會被轉換成Json物件這樣才比較好取值,導致資料在記憶體中至少翻了一倍,這樣是非常耗記憶體和cpu的