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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

springboot执行批量插入_springboot2.1.8+elasticsearch7.3.2(三),添加文档,批量添加文档...

發布時間:2023/12/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot执行批量插入_springboot2.1.8+elasticsearch7.3.2(三),添加文档,批量添加文档... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Service:

/**

* 添加文檔

* @param obj

*/

public String createDoc(Object obj){

/**

* 設置索引,必須是小寫,所以使用toLowerCase()方法進行轉換

*/

IndexRequest request = new IndexRequest(obj.getClass().getSimpleName().toLowerCase());

/**

* 設置文檔id。如果不設置,系統會自動生成

*/

//request.id("xxxx");

request.source(JSONObject.toJSONString(obj), XContentType.JSON);

try {

IndexResponse response = client.index(request, RequestOptions.DEFAULT);

log.info(JSONObject.toJSONString(response));

if(DocWriteResponse.Result.CREATED == response.getResult()){

return response.getId();

}

} catch (IOException e) {

e.printStackTrace();

}

return "";

}

/**

* 批量處理文檔

* @param list 需要處理的文檔列表

*/

public void createMultiDoc(List list){

BulkRequest request = new BulkRequest();

/**

* 遍歷

* IndexRequest不設置id,讓系統自己添加

* DeleteRequest 批量刪除

* UpdateRequest 批量更新

*/

list.forEach((obj)->{

request.add(new IndexRequest(obj.getClass().getSimpleName().toLowerCase())

.source(JSONObject.toJSONString(obj), XContentType.JSON));

});

try {

BulkResponse bulkResponse = client.bulk(request,RequestOptions.DEFAULT);

log.info(JSONObject.toJSONString(bulkResponse));

bulkResponse.forEach((BulkItemResponse bulkItemResponse)->{

DocWriteResponse itemResponse = bulkItemResponse.getResponse();

switch (bulkItemResponse.getOpType()) {

case INDEX:

case CREATE:

IndexResponse indexResponse = (IndexResponse) itemResponse;

log.info(JSONObject.toJSONString(indexResponse));

if(DocWriteResponse.Result.CREATED == indexResponse.getResult()){

log.info("添加成功");

}

break;

case UPDATE:

UpdateResponse updateResponse = (UpdateResponse) itemResponse;

break;

case DELETE:

DeleteResponse deleteResponse = (DeleteResponse) itemResponse;

break;

default:break;

}

});

} catch (IOException e) {

e.printStackTrace();

}

}

測試類:

/**

* 測試添加文檔

*/

@Test

public void createDoc(){

Article article = new Article();

article.setTitle("水調歌頭·明月幾時有");

article.setContent("丙辰中秋,歡飲達旦,大醉,作此篇,兼懷子由。\n" +

"\n" +

"明月幾時有?把酒問青天。不知天上宮闕,今夕是何年。我欲乘風歸去,又恐瓊樓玉宇,高處不勝寒。起舞弄清影,何似在人間?(何似 一作:何時;又恐 一作:惟 / 唯恐)\n" +

"轉朱閣,低綺戶,照無眠。不應有恨,何事長向別時圓?人有悲歡離合,月有陰晴圓缺,此事古難全。但愿人長久,千里共嬋娟。(長向 一作:偏向)");

article.setCreate_time(new Date());

String result = commodityServiceImpl.createDoc(article);

log.info("添加文檔返回的id:{}",result);

}

/**

* 測試批量添加文檔

*/

@Test

public void createMultiDoc(){

Article article1 = new Article();

article1.setTitle("聲聲慢·尋尋覓覓");

article1.setContent("尋尋覓覓,冷冷清清,凄凄慘慘戚戚。乍暖還寒時候,最難將息。三杯兩盞淡酒,怎敵他、晚來風急?雁過也,正傷心,卻是舊時相識。\n" +

"滿地黃花堆積,憔悴損,如今有誰堪摘?守著窗兒,獨自怎生得黑?梧桐更兼細雨,到黃昏、點點滴滴。這次第,怎一個愁字了得!(守著窗兒 一作:守著窗兒)測試春晚");

article1.setCreate_time(new Date());

Article article2 = new Article();

article2.setTitle("卜算子·詠梅");

article2.setContent("驛外斷橋邊,寂寞開無主。已是黃昏獨自愁,更著風和雨。(著 同:著) \n" +

"無意苦爭春,一任群芳妒。零落成泥碾作塵,只有香如故。");

article2.setCreate_time(new Date());

Article article3 = new Article();

article3.setTitle("武陵春·春晚");

article3.setContent("風住塵香花已盡,日晚倦梳頭。物是人非事事休,欲語淚先流。\n" +

"聞說雙溪春尚好,也擬泛輕舟。只恐雙溪舴艋舟,載不動許多愁。測試尋尋覓覓詠梅.測試詠梅");

article3.setCreate_time(new Date());

List list = new ArrayList<>();

list.add(article1);

list.add(article2);

list.add(article3);

commodityServiceImpl.createMultiDoc(list);

}

運行測試類的批量添加之后控制臺輸出:

2019-09-21 00:31:08.468 INFO 230324 --- [ main] c.z.e.service.impl.ServiceImpl : {"fragment":false,"id":"LTODT20Bk6nS0BLhTTX_","index":"article","primaryTerm":1,"result":"CREATED","seqNo":0,"shardId":{"fragment":true,"id":-1,"index":{"fragment":false,"name":"article","uUID":"_na_"},"indexName":"article"},"shardInfo":{"failed":0,"failures":[],"fragment":false,"successful":1,"total":1},"type":"_doc","version":1}

2019-09-21 00:31:08.468 INFO 230324 --- [ main] c.z.e.service.impl.ServiceImpl : 添加成功

2019-09-21 00:31:08.469 INFO 230324 --- [ main] c.z.e.service.impl.ServiceImpl : {"fragment":false,"id":"LjODT20Bk6nS0BLhTTX_","index":"article","primaryTerm":1,"result":"CREATED","seqNo":1,"shardId":{"fragment":true,"id":-1,"index":{"fragment":false,"name":"article","uUID":"_na_"},"indexName":"article"},"shardInfo":{"failed":0,"failures":[],"fragment":false,"successful":1,"total":1},"type":"_doc","version":1}

2019-09-21 00:31:08.469 INFO 230324 --- [ main] c.z.e.service.impl.ServiceImpl : 添加成功

2019-09-21 00:31:08.469 INFO 230324 --- [ main] c.z.e.service.impl.ServiceImpl : {"fragment":false,"id":"LzODT20Bk6nS0BLhTTX_","index":"article","primaryTerm":1,"result":"CREATED","seqNo":2,"shardId":{"fragment":true,"id":-1,"index":{"fragment":false,"name":"article","uUID":"_na_"},"indexName":"article"},"shardInfo":{"failed":0,"failures":[],"fragment":false,"successful":1,"total":1},"type":"_doc","version":1}

2019-09-21 00:31:08.469 INFO 230324 --- [ main] c.z.e.service.impl.ServiceImpl : 添加成功

在kibana中查看mapping,發現實體類Article的create_time也出現了

{

"mapping": {

"properties": {

"content": {

"type": "text",

"analyzer": "ik_max_word"

},

"create_time": {

"type": "long"

},

"title": {

"type": "text",

"analyzer": "ik_max_word"

}

}

}

}

也可以在后臺看到新創建的文檔數量

在Dev Tools 頁面執行GET /article/_search 查看:上面使用批量添加接口添加的文檔已經出現了

總結

以上是生活随笔為你收集整理的springboot执行批量插入_springboot2.1.8+elasticsearch7.3.2(三),添加文档,批量添加文档...的全部內容,希望文章能夠幫你解決所遇到的問題。

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