27
27
import org .springframework .data .elasticsearch .core .query .*;
28
28
import org .springframework .data .util .CloseableIterator ;
29
29
import org .springframework .lang .Nullable ;
30
+ import org .springframework .util .Assert ;
30
31
31
32
/**
32
33
* ElasticsearchOperations
@@ -44,17 +45,19 @@ public interface ElasticsearchOperations {
44
45
* adding new alias
45
46
*
46
47
* @param query
48
+ * @param index
47
49
* @return
48
50
*/
49
- boolean addAlias (AliasQuery query );
51
+ boolean addAlias (AliasQuery query , IndexCoordinates index );
50
52
51
53
/**
52
54
* removing previously created alias
53
55
*
54
56
* @param query
57
+ * @param index
55
58
* @return
56
59
*/
57
- boolean removeAlias (AliasQuery query );
60
+ boolean removeAlias (AliasQuery query , IndexCoordinates index );
58
61
59
62
/**
60
63
* Create an index for a class
@@ -163,13 +166,14 @@ public interface ElasticsearchOperations {
163
166
<T > T query (NativeSearchQuery query , ResultsExtractor <T > resultsExtractor );
164
167
165
168
/**
166
- * Execute the query against elasticsearch and return the first returned object
169
+ * Retrieves an object from an index
167
170
*
168
- * @param query
169
- * @param clazz
170
- * @return the first matching object
171
+ * @param query the query defining the id of the object to get
172
+ * @param clazz the type of the object to be returned
173
+ * @param index the index from which the object is read.
174
+ * @return the found object
171
175
*/
172
- <T > T queryForObject (GetQuery query , Class <T > clazz );
176
+ <T > T get (GetQuery query , Class <T > clazz , IndexCoordinates index );
173
177
174
178
/**
175
179
* Execute the query against elasticsearch and return the first returned object
@@ -178,7 +182,10 @@ public interface ElasticsearchOperations {
178
182
* @param clazz
179
183
* @return the first matching object
180
184
*/
181
- <T > T queryForObject (CriteriaQuery query , Class <T > clazz );
185
+ default <T > T queryForObject (CriteriaQuery query , Class <T > clazz ){
186
+ List <T > content = queryForPage (query , clazz ).getContent ();
187
+ return content .isEmpty () ? null : content .get (0 );
188
+ }
182
189
183
190
/**
184
191
* Execute the query against elasticsearch and return the first returned object
@@ -187,7 +194,10 @@ public interface ElasticsearchOperations {
187
194
* @param clazz
188
195
* @return the first matching object
189
196
*/
190
- <T > T queryForObject (StringQuery query , Class <T > clazz );
197
+ default <T > T queryForObject (StringQuery query , Class <T > clazz ) {
198
+ List <T > content = queryForPage (query , clazz ).getContent ();
199
+ return content .isEmpty () ? null : content .get (0 );
200
+ }
191
201
192
202
/**
193
203
* Execute the query against elasticsearch and return result as {@link Page}
@@ -326,36 +336,23 @@ default List<List<?>> queryForList(List<NativeSearchQuery> queries, List<Class<?
326
336
/**
327
337
* return number of elements found by given query
328
338
*
329
- * @param query
330
- * @param clazz
331
- * @return
332
- */
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
339
+ * @param query the query to execute
340
+ * @param index the index to run the query against
341
+ * @return count
349
342
*/
350
- <T > long count (NativeSearchQuery query , Class <T > clazz );
343
+ default <T > long count (Query query , IndexCoordinates index ) {
344
+ return count (query , null , index );
345
+ }
351
346
352
347
/**
353
348
* return number of elements found by given query
354
349
*
355
- * @param query
356
- * @return
350
+ * @param query the query to execute
351
+ * @param clazz the entity clazz used for property mapping
352
+ * @param index the index to run the query against
353
+ * @return count
357
354
*/
358
- < T > long count (NativeSearchQuery query );
355
+ long count (Query query , @ Nullable Class <?> clazz , IndexCoordinates index );
359
356
360
357
/**
361
358
* Execute a multiGet against elasticsearch for the given ids
@@ -380,7 +377,7 @@ default List<List<?>> queryForList(List<NativeSearchQuery> queries, List<Class<?
380
377
* @param updateQuery
381
378
* @return
382
379
*/
383
- UpdateResponse update (UpdateQuery updateQuery );
380
+ UpdateResponse update (UpdateQuery updateQuery , IndexCoordinates index );
384
381
385
382
/**
386
383
* Bulk index all objects. Will do save or update.
@@ -405,8 +402,8 @@ default void bulkIndex(List<IndexQuery> queries) {
405
402
*
406
403
* @param queries the queries to execute in bulk
407
404
*/
408
- default void bulkUpdate (List <UpdateQuery > queries ) {
409
- bulkUpdate (queries , BulkOptions .defaultOptions ());
405
+ default void bulkUpdate (List <UpdateQuery > queries , IndexCoordinates index ) {
406
+ bulkUpdate (queries , BulkOptions .defaultOptions (), index );
410
407
}
411
408
412
409
/**
@@ -416,7 +413,7 @@ default void bulkUpdate(List<UpdateQuery> queries) {
416
413
* @param bulkOptions options to be added to the bulk request
417
414
* @since 3.2
418
415
*/
419
- void bulkUpdate (List <UpdateQuery > queries , BulkOptions bulkOptions );
416
+ void bulkUpdate (List <UpdateQuery > queries , BulkOptions bulkOptions , IndexCoordinates index );
420
417
421
418
/**
422
419
* Delete the one object with provided id
@@ -504,18 +501,20 @@ default void bulkUpdate(List<UpdateQuery> queries) {
504
501
boolean typeExists (String index , String type );
505
502
506
503
/**
507
- * refresh the index
504
+ * refresh the index(es)
508
505
*
509
- * @param indexName
506
+ * @param indexNames
510
507
*/
511
- void refresh (String indexName );
508
+ void refresh (String ... indexNames );
512
509
513
510
/**
514
511
* refresh the index
515
512
*
516
513
* @param clazz
517
514
*/
518
- <T > void refresh (Class <T > clazz );
515
+ default <T > void refresh (Class <T > clazz ) {
516
+ refresh (getIndexCoordinatesFor (clazz ).getIndexNames ());
517
+ }
519
518
520
519
/**
521
520
* Returns scrolled page for given query
@@ -564,4 +563,15 @@ default void bulkUpdate(List<UpdateQuery> queries) {
564
563
* @return Converter in use
565
564
*/
566
565
ElasticsearchConverter getElasticsearchConverter ();
566
+
567
+ /**
568
+ * @since 4.0
569
+ */
570
+ RequestFactory getRequestFactory ();
571
+
572
+ default IndexCoordinates getIndexCoordinatesFor (Class <?> clazz ) {
573
+ ElasticsearchPersistentEntity entity = getPersistentEntityFor (clazz );
574
+ return IndexCoordinates .of (entity .getIndexName (), entity .getIndexType ());
575
+ }
576
+
567
577
}
0 commit comments