@@ -280,7 +280,7 @@ public <T extends BaseEsEntity> T save(String index, String type, T entity) thro
280
280
|| response .getResult () == DocWriteResponse .Result .UPDATED ) {
281
281
return entity ;
282
282
} else {
283
- log .warn ("【ES】save 响应结果无效! result: {}" , response .getResult ());
283
+ log .warn ("【ES】save 失败, result: {}! " , response .getResult ());
284
284
return null ;
285
285
}
286
286
}
@@ -292,45 +292,25 @@ public <T extends BaseEsEntity> boolean saveBatch(String index, String type, Col
292
292
return true ;
293
293
}
294
294
295
- BulkRequest bulkRequest = new BulkRequest ();
296
- bulkRequest .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
297
- for (T entity : list ) {
298
- Map <String , Object > map = toMap (entity );
299
- if (MapUtil .isEmpty (map )) {
300
- continue ;
301
- }
302
- IndexRequest request = new IndexRequest (index , type ).source (map );
303
- if (entity .getDocId () != null ) {
304
- request .id (entity .getDocId ());
305
- }
306
- bulkRequest .add (request );
307
- }
308
-
295
+ BulkRequest bulkRequest = toBulkIndexRequest (index , type , list );
309
296
BulkResponse response = client .bulk (bulkRequest , RequestOptions .DEFAULT );
310
- return response != null && !response .hasFailures ();
297
+ if (response == null ) {
298
+ log .warn ("【ES】saveBatch 失败,result 为空!list: {}" , JsonUtil .toString (list ));
299
+ return false ;
300
+ }
301
+ if (response .hasFailures ()) {
302
+ log .warn ("【ES】saveBatch 失败,result: {}!" , response .buildFailureMessage ());
303
+ return false ;
304
+ }
305
+ return true ;
311
306
}
312
307
313
308
public <T extends BaseEsEntity > void asyncSaveBatch (String index , String type , Collection <T > list ,
314
309
ActionListener <BulkResponse > listener ) {
315
-
316
310
if (CollectionUtil .isEmpty (list )) {
317
311
return ;
318
312
}
319
-
320
- BulkRequest bulkRequest = new BulkRequest ();
321
- bulkRequest .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
322
- for (T entity : list ) {
323
- Map <String , Object > map = toMap (entity );
324
- if (MapUtil .isEmpty (map )) {
325
- continue ;
326
- }
327
- IndexRequest request = new IndexRequest (index , type ).source (map );
328
- if (entity .getDocId () != null ) {
329
- request .id (entity .getDocId ());
330
- }
331
- bulkRequest .add (request );
332
- }
333
-
313
+ BulkRequest bulkRequest = toBulkIndexRequest (index , type , list );
334
314
client .bulkAsync (bulkRequest , RequestOptions .DEFAULT , listener );
335
315
}
336
316
@@ -362,7 +342,7 @@ public <T extends BaseEsEntity> T updateById(String index, String type, T entity
362
342
if (response .getResult () == DocWriteResponse .Result .UPDATED ) {
363
343
return entity ;
364
344
} else {
365
- log .warn ("【ES】updateById 响应结果无效! result: {}" , response .getResult ());
345
+ log .warn ("【ES】updateById 响应结果无效, result: {}! " , response .getResult ());
366
346
return null ;
367
347
}
368
348
}
@@ -374,23 +354,49 @@ public <T extends BaseEsEntity> boolean updateBatchIds(String index, String type
374
354
return true ;
375
355
}
376
356
377
- BulkRequest bulkRequest = toUpdateBulkRequest (index , type , list );
357
+ BulkRequest bulkRequest = toBulkUpdateRequest (index , type , list );
378
358
BulkResponse response = client .bulk (bulkRequest , RequestOptions .DEFAULT );
379
- return response != null && !response .hasFailures ();
359
+ if (response == null ) {
360
+ log .warn ("【ES】updateBatchIds 失败,result 为空!list: {}" , JsonUtil .toString (list ));
361
+ return false ;
362
+ }
363
+ if (response .hasFailures ()) {
364
+ log .warn ("【ES】updateBatchIds 失败,result: {}!" , response .buildFailureMessage ());
365
+ return false ;
366
+ }
367
+ return true ;
380
368
}
381
369
382
370
public <T extends BaseEsEntity > void asyncUpdateBatchIds (String index , String type , Collection <T > list ,
383
371
ActionListener <BulkResponse > listener ) {
384
-
385
372
if (CollectionUtil .isEmpty (list )) {
386
373
return ;
387
374
}
388
-
389
- BulkRequest bulkRequest = toUpdateBulkRequest (index , type , list );
375
+ BulkRequest bulkRequest = toBulkUpdateRequest (index , type , list );
390
376
client .bulkAsync (bulkRequest , RequestOptions .DEFAULT , listener );
391
377
}
392
378
393
- private <T extends BaseEsEntity > BulkRequest toUpdateBulkRequest (String index , String type , Collection <T > list ) {
379
+ private <T extends BaseEsEntity > BulkRequest toBulkIndexRequest (String index , String type , Collection <T > list ) {
380
+ BulkRequest bulkRequest = new BulkRequest ();
381
+ bulkRequest .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
382
+ for (T entity : list ) {
383
+ if (entity == null ) {
384
+ continue ;
385
+ }
386
+ Map <String , Object > map = toMap (entity );
387
+ if (MapUtil .isEmpty (map )) {
388
+ continue ;
389
+ }
390
+ IndexRequest request = new IndexRequest (index , type ).source (map );
391
+ if (entity .getDocId () != null ) {
392
+ request .id (entity .getDocId ());
393
+ }
394
+ bulkRequest .add (request );
395
+ }
396
+ return bulkRequest ;
397
+ }
398
+
399
+ private <T extends BaseEsEntity > BulkRequest toBulkUpdateRequest (String index , String type , Collection <T > list ) {
394
400
BulkRequest bulkRequest = new BulkRequest ();
395
401
bulkRequest .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
396
402
for (T entity : list ) {
@@ -426,11 +432,14 @@ public boolean deleteBatchIds(String index, String type, Collection<String> ids)
426
432
427
433
BulkResponse response = client .bulk (bulkRequest , RequestOptions .DEFAULT );
428
434
if (response == null ) {
429
- log .warn ("【ES】batchDeleteById 响应结果为空!" );
435
+ log .warn ("【ES】deleteBatchIds 失败,result 为空!ids: {}" , JsonUtil . toString ( ids ) );
430
436
return false ;
431
437
}
432
-
433
- return !response .hasFailures ();
438
+ if (response .hasFailures ()) {
439
+ log .warn ("【ES】deleteBatchIds 失败,result: {}!" , response .buildFailureMessage ());
440
+ return false ;
441
+ }
442
+ return true ;
434
443
}
435
444
436
445
public void asyncDeleteBatchIds (String index , String type , Collection <String > ids ,
@@ -568,7 +577,8 @@ public <T> PageData<T> pojoPage(String index, String type, int from, int size, Q
568
577
/**
569
578
* search after 分页
570
579
*/
571
- public <T extends BaseEsEntity > ScrollData <T > pojoPageByScrollId (String index , String type , String scrollId , int size ,
580
+ public <T extends BaseEsEntity > ScrollData <T > pojoPageByScrollId (String index , String type , String scrollId ,
581
+ int size ,
572
582
QueryBuilder queryBuilder , Class <T > clazz ) throws IOException {
573
583
574
584
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder ();
0 commit comments