@@ -44,17 +44,19 @@ public interface ElasticsearchOperations {
44
44
* adding new alias
45
45
*
46
46
* @param query
47
+ * @param index
47
48
* @return
48
49
*/
49
- boolean addAlias (AliasQuery query );
50
+ boolean addAlias (AliasQuery query , IndexCoordinates index );
50
51
51
52
/**
52
53
* removing previously created alias
53
54
*
54
55
* @param query
56
+ * @param index
55
57
* @return
56
58
*/
57
- boolean removeAlias (AliasQuery query );
59
+ boolean removeAlias (AliasQuery query , IndexCoordinates index );
58
60
59
61
/**
60
62
* Create an index for a class
@@ -126,17 +128,17 @@ public interface ElasticsearchOperations {
126
128
* Get mapping for a class
127
129
*
128
130
* @param clazz
129
- * @param <T>
130
131
*/
131
- <T > Map <String , Object > getMapping (Class <T > clazz );
132
+ default Map <String , Object > getMapping (Class <?> clazz ) {
133
+ return getMapping (getIndexCoordinatesFor (clazz ));
134
+ }
132
135
133
136
/**
134
- * Get mapping for a given indexName and type
137
+ * Get mapping for a given index coordinates
135
138
*
136
- * @param indexName
137
- * @param type
139
+ * @param index
138
140
*/
139
- Map <String , Object > getMapping (String indexName , String type );
141
+ Map <String , Object > getMapping (IndexCoordinates index );
140
142
141
143
/**
142
144
* Get settings for a given indexName
@@ -163,13 +165,14 @@ public interface ElasticsearchOperations {
163
165
<T > T query (NativeSearchQuery query , ResultsExtractor <T > resultsExtractor );
164
166
165
167
/**
166
- * Execute the query against elasticsearch and return the first returned object
168
+ * Retrieves an object from an index
167
169
*
168
- * @param query
169
- * @param clazz
170
- * @return the first matching object
170
+ * @param query the query defining the id of the object to get
171
+ * @param clazz the type of the object to be returned
172
+ * @param index the index from which the object is read.
173
+ * @return the found object
171
174
*/
172
- <T > T queryForObject (GetQuery query , Class <T > clazz );
175
+ <T > T get (GetQuery query , Class <T > clazz , IndexCoordinates index );
173
176
174
177
/**
175
178
* Execute the query against elasticsearch and return the first returned object
@@ -178,7 +181,10 @@ public interface ElasticsearchOperations {
178
181
* @param clazz
179
182
* @return the first matching object
180
183
*/
181
- <T > T queryForObject (CriteriaQuery query , Class <T > clazz );
184
+ default <T > T queryForObject (CriteriaQuery query , Class <T > clazz ) {
185
+ List <T > content = queryForPage (query , clazz ).getContent ();
186
+ return content .isEmpty () ? null : content .get (0 );
187
+ }
182
188
183
189
/**
184
190
* Execute the query against elasticsearch and return the first returned object
@@ -187,7 +193,10 @@ public interface ElasticsearchOperations {
187
193
* @param clazz
188
194
* @return the first matching object
189
195
*/
190
- <T > T queryForObject (StringQuery query , Class <T > clazz );
196
+ default <T > T queryForObject (StringQuery query , Class <T > clazz ) {
197
+ List <T > content = queryForPage (query , clazz ).getContent ();
198
+ return content .isEmpty () ? null : content .get (0 );
199
+ }
191
200
192
201
/**
193
202
* Execute the query against elasticsearch and return result as {@link Page}
@@ -326,36 +335,23 @@ default List<List<?>> queryForList(List<NativeSearchQuery> queries, List<Class<?
326
335
/**
327
336
* return number of elements found by given query
328
337
*
329
- * @param query
330
- * @param clazz
331
- * @return
338
+ * @param query the query to execute
339
+ * @param index the index to run the query against
340
+ * @return count
332
341
*/
333
- <T > long count (CriteriaQuery query , Class <T > clazz );
334
-
335
- /**
336
- * return number of elements found by given query
337
- *
338
- * @param query
339
- * @return
340
- */
341
- <T > long count (CriteriaQuery query );
342
-
343
- /**
344
- * return number of elements found by given query
345
- *
346
- * @param query
347
- * @param clazz
348
- * @return
349
- */
350
- <T > long count (NativeSearchQuery query , Class <T > clazz );
342
+ default <T > long count (Query query , IndexCoordinates index ) {
343
+ return count (query , null , index );
344
+ }
351
345
352
346
/**
353
347
* return number of elements found by given query
354
348
*
355
- * @param query
356
- * @return
349
+ * @param query the query to execute
350
+ * @param clazz the entity clazz used for property mapping
351
+ * @param index the index to run the query against
352
+ * @return count
357
353
*/
358
- < T > long count (NativeSearchQuery query );
354
+ long count (Query query , @ Nullable Class <?> clazz , IndexCoordinates index );
359
355
360
356
/**
361
357
* Execute a multiGet against elasticsearch for the given ids
@@ -372,23 +368,23 @@ default List<List<?>> queryForList(List<NativeSearchQuery> queries, List<Class<?
372
368
* @param query
373
369
* @return returns the document id
374
370
*/
375
- String index (IndexQuery query );
371
+ String index (IndexQuery query , IndexCoordinates index );
376
372
377
373
/**
378
374
* Partial update of the document
379
375
*
380
376
* @param updateQuery
381
377
* @return
382
378
*/
383
- UpdateResponse update (UpdateQuery updateQuery );
379
+ UpdateResponse update (UpdateQuery updateQuery , IndexCoordinates index );
384
380
385
381
/**
386
382
* Bulk index all objects. Will do save or update.
387
383
*
388
384
* @param queries the queries to execute in bulk
389
385
*/
390
- default void bulkIndex (List <IndexQuery > queries ) {
391
- bulkIndex (queries , BulkOptions .defaultOptions ());
386
+ default void bulkIndex (List <IndexQuery > queries , IndexCoordinates index ) {
387
+ bulkIndex (queries , BulkOptions .defaultOptions (), index );
392
388
}
393
389
394
390
/**
@@ -398,15 +394,15 @@ default void bulkIndex(List<IndexQuery> queries) {
398
394
* @param bulkOptions options to be added to the bulk request
399
395
* @since 3.2
400
396
*/
401
- void bulkIndex (List <IndexQuery > queries , BulkOptions bulkOptions );
397
+ void bulkIndex (List <IndexQuery > queries , BulkOptions bulkOptions , IndexCoordinates index );
402
398
403
399
/**
404
400
* Bulk update all objects. Will do update
405
401
*
406
402
* @param queries the queries to execute in bulk
407
403
*/
408
- default void bulkUpdate (List <UpdateQuery > queries ) {
409
- bulkUpdate (queries , BulkOptions .defaultOptions ());
404
+ default void bulkUpdate (List <UpdateQuery > queries , IndexCoordinates index ) {
405
+ bulkUpdate (queries , BulkOptions .defaultOptions (), index );
410
406
}
411
407
412
408
/**
@@ -416,58 +412,42 @@ default void bulkUpdate(List<UpdateQuery> queries) {
416
412
* @param bulkOptions options to be added to the bulk request
417
413
* @since 3.2
418
414
*/
419
- void bulkUpdate (List <UpdateQuery > queries , BulkOptions bulkOptions );
415
+ void bulkUpdate (List <UpdateQuery > queries , BulkOptions bulkOptions , IndexCoordinates index );
420
416
421
417
/**
422
- * Delete the one object with provided id
418
+ * Delete the one object with provided id.
423
419
*
424
- * @param indexName
425
- * @param type
426
- * @param id
420
+ * @param id the document ot delete
421
+ * @param index the index from which to delete
427
422
* @return documentId of the document deleted
428
423
*/
429
- String delete (String indexName , String type , String id );
424
+ String delete (String id , IndexCoordinates index );
430
425
431
426
/**
432
427
* Delete all records matching the criteria
433
428
*
434
- * @param clazz
435
429
* @param criteriaQuery
430
+ * @param index
436
431
*/
437
- <T > void delete (CriteriaQuery criteriaQuery , Class <T > clazz );
438
-
439
- /**
440
- * Delete the one object with provided id
441
- *
442
- * @param clazz
443
- * @param id
444
- * @return documentId of the document deleted
445
- */
446
- <T > String delete (Class <T > clazz , String id );
447
-
448
- /**
449
- * Delete all records matching the query
450
- *
451
- * @param clazz
452
- * @param query
453
- */
454
- <T > void delete (DeleteQuery query , Class <T > clazz );
432
+ void delete (CriteriaQuery criteriaQuery , IndexCoordinates index );
455
433
456
434
/**
457
435
* Delete all records matching the query
458
436
*
459
437
* @param query
438
+ * @param index the index where to delete the records
460
439
*/
461
- void delete (DeleteQuery query );
440
+ void delete (DeleteQuery query , IndexCoordinates index );
462
441
463
442
/**
464
443
* Deletes an index for given entity
465
444
*
466
445
* @param clazz
467
- * @param <T>
468
446
* @return
469
447
*/
470
- <T > boolean deleteIndex (Class <T > clazz );
448
+ default boolean deleteIndex (Class <?> clazz ) {
449
+ return deleteIndex (getPersistentEntityFor (clazz ).getIndexName ());
450
+ }
471
451
472
452
/**
473
453
* Deletes an index for given indexName
@@ -481,10 +461,11 @@ default void bulkUpdate(List<UpdateQuery> queries) {
481
461
* check if index is exists
482
462
*
483
463
* @param clazz
484
- * @param <T>
485
464
* @return
486
465
*/
487
- <T > boolean indexExists (Class <T > clazz );
466
+ default boolean indexExists (Class <?> clazz ) {
467
+ return indexExists (getIndexCoordinatesFor (clazz ).getIndexName ());
468
+ }
488
469
489
470
/**
490
471
* check if index is exists for given IndexName
@@ -495,27 +476,20 @@ default void bulkUpdate(List<UpdateQuery> queries) {
495
476
boolean indexExists (String indexName );
496
477
497
478
/**
498
- * check if type is exists in an index
479
+ * refresh the index(es)
499
480
*
500
- * @param index
501
- * @param type
502
- * @return
481
+ * @param indexNames
503
482
*/
504
- boolean typeExists (String index , String type );
505
-
506
- /**
507
- * refresh the index
508
- *
509
- * @param indexName
510
- */
511
- void refresh (String indexName );
483
+ void refresh (String ... indexNames );
512
484
513
485
/**
514
486
* refresh the index
515
487
*
516
488
* @param clazz
517
489
*/
518
- <T > void refresh (Class <T > clazz );
490
+ default <T > void refresh (Class <T > clazz ) {
491
+ refresh (getIndexCoordinatesFor (clazz ).getIndexNames ());
492
+ }
519
493
520
494
/**
521
495
* Returns scrolled page for given query
@@ -564,4 +538,20 @@ default void bulkUpdate(List<UpdateQuery> queries) {
564
538
* @return Converter in use
565
539
*/
566
540
ElasticsearchConverter getElasticsearchConverter ();
541
+
542
+ /**
543
+ * @since 4.0
544
+ */
545
+ RequestFactory getRequestFactory ();
546
+
547
+ /**
548
+ * @param clazz
549
+ * @return the IndexCoordinates defined on the entity.
550
+ * @since 4.0
551
+ */
552
+ default IndexCoordinates getIndexCoordinatesFor (Class <?> clazz ) {
553
+ ElasticsearchPersistentEntity entity = getPersistentEntityFor (clazz );
554
+ return IndexCoordinates .of (entity .getIndexName (), entity .getIndexType ());
555
+ }
556
+
567
557
}
0 commit comments