@@ -280,7 +280,7 @@ public <T extends BaseEsEntity> T save(String index, String type, T entity) thro
280280 || response .getResult () == DocWriteResponse .Result .UPDATED ) {
281281 return entity ;
282282 } else {
283- log .warn ("【ES】save 响应结果无效! result: {}" , response .getResult ());
283+ log .warn ("【ES】save 失败, result: {}! " , response .getResult ());
284284 return null ;
285285 }
286286 }
@@ -292,45 +292,25 @@ public <T extends BaseEsEntity> boolean saveBatch(String index, String type, Col
292292 return true ;
293293 }
294294
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 );
309296 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 ;
311306 }
312307
313308 public <T extends BaseEsEntity > void asyncSaveBatch (String index , String type , Collection <T > list ,
314309 ActionListener <BulkResponse > listener ) {
315-
316310 if (CollectionUtil .isEmpty (list )) {
317311 return ;
318312 }
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 );
334314 client .bulkAsync (bulkRequest , RequestOptions .DEFAULT , listener );
335315 }
336316
@@ -362,7 +342,7 @@ public <T extends BaseEsEntity> T updateById(String index, String type, T entity
362342 if (response .getResult () == DocWriteResponse .Result .UPDATED ) {
363343 return entity ;
364344 } else {
365- log .warn ("【ES】updateById 响应结果无效! result: {}" , response .getResult ());
345+ log .warn ("【ES】updateById 响应结果无效, result: {}! " , response .getResult ());
366346 return null ;
367347 }
368348 }
@@ -374,23 +354,49 @@ public <T extends BaseEsEntity> boolean updateBatchIds(String index, String type
374354 return true ;
375355 }
376356
377- BulkRequest bulkRequest = toUpdateBulkRequest (index , type , list );
357+ BulkRequest bulkRequest = toBulkUpdateRequest (index , type , list );
378358 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 ;
380368 }
381369
382370 public <T extends BaseEsEntity > void asyncUpdateBatchIds (String index , String type , Collection <T > list ,
383371 ActionListener <BulkResponse > listener ) {
384-
385372 if (CollectionUtil .isEmpty (list )) {
386373 return ;
387374 }
388-
389- BulkRequest bulkRequest = toUpdateBulkRequest (index , type , list );
375+ BulkRequest bulkRequest = toBulkUpdateRequest (index , type , list );
390376 client .bulkAsync (bulkRequest , RequestOptions .DEFAULT , listener );
391377 }
392378
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 ) {
394400 BulkRequest bulkRequest = new BulkRequest ();
395401 bulkRequest .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
396402 for (T entity : list ) {
@@ -426,11 +432,14 @@ public boolean deleteBatchIds(String index, String type, Collection<String> ids)
426432
427433 BulkResponse response = client .bulk (bulkRequest , RequestOptions .DEFAULT );
428434 if (response == null ) {
429- log .warn ("【ES】batchDeleteById 响应结果为空!" );
435+ log .warn ("【ES】deleteBatchIds 失败,result 为空!ids: {}" , JsonUtil . toString ( ids ) );
430436 return false ;
431437 }
432-
433- return !response .hasFailures ();
438+ if (response .hasFailures ()) {
439+ log .warn ("【ES】deleteBatchIds 失败,result: {}!" , response .buildFailureMessage ());
440+ return false ;
441+ }
442+ return true ;
434443 }
435444
436445 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
568577 /**
569578 * search after 分页
570579 */
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 ,
572582 QueryBuilder queryBuilder , Class <T > clazz ) throws IOException {
573583
574584 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder ();
0 commit comments