Elasticsearch bulk 批量操作文档

 

打开、关闭连接代价比较高,尽量把多个操作放在一个请求中完成,来提升性能。

 

 

批量插入文档

POST mall/_doc/_bulk
{ "index":{} }
{ "goods_name":"苹果","goods_price":10.0,"goods_description":"新鲜苹果,10元一斤" }
{ "index":{} }
{ "goods_name":"梨子","goods_price":5.0,"goods_description":"新鲜梨子,5元一斤" }

2行表示一个文档

在url中指定index、type,文档id自动生成。

 

 

也可以手动指定文档id:

POST mall/_doc/_bulk
{ "index":{"_id":"1"} }
{ "goods_name":"苹果","goods_price":10.0,"goods_description":"新鲜苹果,10元一斤" }
{ "index":{"_id":"2"} }
{ "goods_name":"梨子","goods_price":5.0,"goods_description":"新鲜梨子,5元一斤" }

 

 

index、type、id都可以写在“index”中:

POST _bulk
{ "index":{"_index":"mall","_type":"_doc","_id":"1"} }
{ "goods_name":"冬瓜","goods_price":5.0,"goods_description":"新鲜冬瓜,5元一斤" }
{ "index":{"_index":"mall","_type":"_doc","_id":"2"} }
{ "goods_name":"西瓜","goods_price":5.0,"goods_description":"新鲜西瓜,5元一斤" }

要么写在“index”中,要么写在url中,总之要指定index、type。在“index”中指定时有前缀_

id可以不指定,会自动生成。

 

 

用PUT、POST都行,对document来说是新建,对index、type来说是更新。

 

posted @ 2020-04-18 16:54  chy_18883701161  阅读(983)  评论(0编辑  收藏  举报