27
27
import org .elasticsearch .index .query .QueryBuilder ;
28
28
import org .slf4j .Logger ;
29
29
import org .slf4j .LoggerFactory ;
30
+
30
31
import org .springframework .data .domain .Page ;
31
32
import org .springframework .data .domain .PageImpl ;
32
33
import org .springframework .data .domain .PageRequest ;
37
38
import org .springframework .data .elasticsearch .core .SearchHit ;
38
39
import org .springframework .data .elasticsearch .core .SearchHitSupport ;
39
40
import org .springframework .data .elasticsearch .core .SearchHits ;
40
- import org .springframework .data .elasticsearch .core .SearchPage ;
41
41
import org .springframework .data .elasticsearch .core .aggregation .AggregatedPage ;
42
42
import org .springframework .data .elasticsearch .core .mapping .ElasticsearchPersistentEntity ;
43
43
import org .springframework .data .elasticsearch .core .mapping .IndexCoordinates ;
50
50
import org .springframework .data .util .Streamable ;
51
51
import org .springframework .lang .Nullable ;
52
52
import org .springframework .util .Assert ;
53
+ import org .springframework .util .CollectionUtils ;
53
54
54
55
/**
55
56
* Elasticsearch specific repository implementation. Likely to be used as target within
@@ -151,14 +152,12 @@ public Iterable<T> findAllById(Iterable<ID> ids) {
151
152
Assert .notNull (ids , "ids can't be null." );
152
153
153
154
List <T > result = new ArrayList <>();
154
- List <String > stringIds = stringIdsRepresentation (ids );
155
-
156
- if (stringIds .isEmpty ()) {
155
+ Query idQuery = getIdQuery (ids );
156
+ if (CollectionUtils .isEmpty (idQuery .getIds ())) {
157
157
return result ;
158
158
}
159
159
160
- NativeSearchQuery query = new NativeSearchQueryBuilder ().withIds (stringIds ).build ();
161
- List <T > multiGetEntities = execute (operations -> operations .multiGet (query , entityClass , getIndexCoordinates ()));
160
+ List <T > multiGetEntities = execute (operations -> operations .multiGet (idQuery , entityClass , getIndexCoordinates ()));
162
161
163
162
if (multiGetEntities != null ) {
164
163
multiGetEntities .forEach (entity -> {
@@ -291,54 +290,41 @@ public void delete(T entity) {
291
290
}
292
291
293
292
@ Override
294
- public void deleteAll (Iterable <? extends T > entities ) {
293
+ public void deleteAllById (Iterable <? extends ID > ids ) {
295
294
296
- Assert .notNull (entities , "Cannot delete 'null' list." );
295
+ Assert .notNull (ids , "Cannot delete 'null' list." );
297
296
298
297
IndexCoordinates indexCoordinates = getIndexCoordinates ();
299
298
IdsQueryBuilder idsQueryBuilder = idsQuery ();
300
- for (T entity : entities ) {
301
- ID id = extractIdFromBean (entity );
302
- if (id != null ) {
303
- idsQueryBuilder .addIds (stringIdRepresentation (id ));
304
- }
299
+ for (ID id : ids ) {
300
+ idsQueryBuilder .addIds (stringIdRepresentation (id ));
305
301
}
306
302
307
303
if (idsQueryBuilder .ids ().isEmpty ()) {
308
304
return ;
309
305
}
310
306
311
- Query query = new NativeSearchQueryBuilder ().withQuery (idsQueryBuilder ).build ();
312
-
313
307
executeAndRefresh ((OperationsCallback <Void >) operations -> {
314
- operations .delete (query , entityClass , indexCoordinates );
308
+ operations .delete (new NativeSearchQueryBuilder ().withQuery (idsQueryBuilder ).build (), entityClass ,
309
+ indexCoordinates );
315
310
return null ;
316
311
});
317
312
}
318
313
319
314
@ Override
320
- public void deleteAllById (Iterable <? extends ID > ids ) {
315
+ public void deleteAll (Iterable <? extends T > entities ) {
321
316
322
- Assert .notNull (ids , "Cannot delete 'null' list." );
317
+ Assert .notNull (entities , "Cannot delete 'null' list." );
323
318
324
- IndexCoordinates indexCoordinates = getIndexCoordinates ();
325
- IdsQueryBuilder idsQueryBuilder = idsQuery ();
326
- for ( ID id : ids ) {
319
+ List < ID > ids = new ArrayList <> ();
320
+ for ( T entity : entities ) {
321
+ ID id = extractIdFromBean ( entity );
327
322
if (id != null ) {
328
- idsQueryBuilder . addIds ( stringIdRepresentation ( id ) );
323
+ ids . add ( id );
329
324
}
330
325
}
331
326
332
- if (idsQueryBuilder .ids ().isEmpty ()) {
333
- return ;
334
- }
335
-
336
- Query query = new NativeSearchQueryBuilder ().withQuery (idsQueryBuilder ).build ();
337
-
338
- executeAndRefresh ((OperationsCallback <Void >) operations -> {
339
- operations .delete (query , entityClass , indexCoordinates );
340
- return null ;
341
- });
327
+ deleteAllById (ids );
342
328
}
343
329
344
330
private void doDelete (@ Nullable ID id , @ Nullable String routing , IndexCoordinates indexCoordinates ) {
@@ -369,7 +355,7 @@ protected ID extractIdFromBean(T entity) {
369
355
return entityInformation .getId (entity );
370
356
}
371
357
372
- private List <String > stringIdsRepresentation (Iterable <ID > ids ) {
358
+ private List <String > stringIdsRepresentation (Iterable <? extends ID > ids ) {
373
359
374
360
Assert .notNull (ids , "ids can't be null." );
375
361
@@ -385,6 +371,12 @@ private IndexCoordinates getIndexCoordinates() {
385
371
return operations .getIndexCoordinatesFor (entityClass );
386
372
}
387
373
374
+ private Query getIdQuery (Iterable <? extends ID > ids ) {
375
+ List <String > stringIds = stringIdsRepresentation (ids );
376
+
377
+ return new NativeSearchQueryBuilder ().withIds (stringIds ).build ();
378
+ }
379
+
388
380
// region operations callback
389
381
@ FunctionalInterface
390
382
public interface OperationsCallback <R > {
0 commit comments