Skip to content

Commit 6e175e5

Browse files
committed
DATAES-631 - Consolidate query objects.
1 parent 3f82108 commit 6e175e5

26 files changed

+767
-607
lines changed

src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
99
import org.springframework.data.elasticsearch.core.index.MappingBuilder;
1010
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
11+
import org.springframework.util.Assert;
1112
import org.springframework.util.StringUtils;
1213

1314
/**
@@ -21,6 +22,17 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
2122
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractElasticsearchTemplate.class);
2223

2324
protected ElasticsearchConverter elasticsearchConverter;
25+
protected RequestFactory requestFactory;
26+
27+
public RequestFactory getRequestFactory() {
28+
return requestFactory;
29+
}
30+
31+
protected void initialize(ElasticsearchConverter elasticsearchConverter) {
32+
Assert.notNull(elasticsearchConverter, "elasticsearchConverter must not be null.");
33+
this.elasticsearchConverter = elasticsearchConverter;
34+
this.requestFactory = new RequestFactory(elasticsearchConverter);
35+
}
2436

2537
protected ElasticsearchConverter createElasticsearchConverter() {
2638
MappingElasticsearchConverter mappingElasticsearchConverter = new MappingElasticsearchConverter(

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java

+51-41
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.data.elasticsearch.core.query.*;
2828
import org.springframework.data.util.CloseableIterator;
2929
import org.springframework.lang.Nullable;
30+
import org.springframework.util.Assert;
3031

3132
/**
3233
* ElasticsearchOperations
@@ -44,17 +45,19 @@ public interface ElasticsearchOperations {
4445
* adding new alias
4546
*
4647
* @param query
48+
* @param index
4749
* @return
4850
*/
49-
boolean addAlias(AliasQuery query);
51+
boolean addAlias(AliasQuery query, IndexCoordinates index);
5052

5153
/**
5254
* removing previously created alias
5355
*
5456
* @param query
57+
* @param index
5558
* @return
5659
*/
57-
boolean removeAlias(AliasQuery query);
60+
boolean removeAlias(AliasQuery query, IndexCoordinates index);
5861

5962
/**
6063
* Create an index for a class
@@ -163,13 +166,14 @@ public interface ElasticsearchOperations {
163166
<T> T query(NativeSearchQuery query, ResultsExtractor<T> resultsExtractor);
164167

165168
/**
166-
* Execute the query against elasticsearch and return the first returned object
169+
* Retrieves an object from an index
167170
*
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
171175
*/
172-
<T> T queryForObject(GetQuery query, Class<T> clazz);
176+
<T> T get(GetQuery query, Class<T> clazz, IndexCoordinates index);
173177

174178
/**
175179
* Execute the query against elasticsearch and return the first returned object
@@ -178,7 +182,10 @@ public interface ElasticsearchOperations {
178182
* @param clazz
179183
* @return the first matching object
180184
*/
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+
}
182189

183190
/**
184191
* Execute the query against elasticsearch and return the first returned object
@@ -187,7 +194,10 @@ public interface ElasticsearchOperations {
187194
* @param clazz
188195
* @return the first matching object
189196
*/
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+
}
191201

192202
/**
193203
* Execute the query against elasticsearch and return result as {@link Page}
@@ -326,36 +336,23 @@ default List<List<?>> queryForList(List<NativeSearchQuery> queries, List<Class<?
326336
/**
327337
* return number of elements found by given query
328338
*
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
349342
*/
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+
}
351346

352347
/**
353348
* return number of elements found by given query
354349
*
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
357354
*/
358-
<T> long count(NativeSearchQuery query);
355+
long count(Query query, @Nullable Class<?> clazz, IndexCoordinates index);
359356

360357
/**
361358
* Execute a multiGet against elasticsearch for the given ids
@@ -380,7 +377,7 @@ default List<List<?>> queryForList(List<NativeSearchQuery> queries, List<Class<?
380377
* @param updateQuery
381378
* @return
382379
*/
383-
UpdateResponse update(UpdateQuery updateQuery);
380+
UpdateResponse update(UpdateQuery updateQuery, IndexCoordinates index);
384381

385382
/**
386383
* Bulk index all objects. Will do save or update.
@@ -405,8 +402,8 @@ default void bulkIndex(List<IndexQuery> queries) {
405402
*
406403
* @param queries the queries to execute in bulk
407404
*/
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);
410407
}
411408

412409
/**
@@ -416,7 +413,7 @@ default void bulkUpdate(List<UpdateQuery> queries) {
416413
* @param bulkOptions options to be added to the bulk request
417414
* @since 3.2
418415
*/
419-
void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions);
416+
void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions, IndexCoordinates index);
420417

421418
/**
422419
* Delete the one object with provided id
@@ -504,18 +501,20 @@ default void bulkUpdate(List<UpdateQuery> queries) {
504501
boolean typeExists(String index, String type);
505502

506503
/**
507-
* refresh the index
504+
* refresh the index(es)
508505
*
509-
* @param indexName
506+
* @param indexNames
510507
*/
511-
void refresh(String indexName);
508+
void refresh(String... indexNames);
512509

513510
/**
514511
* refresh the index
515512
*
516513
* @param clazz
517514
*/
518-
<T> void refresh(Class<T> clazz);
515+
default <T> void refresh(Class<T> clazz) {
516+
refresh(getIndexCoordinatesFor(clazz).getIndexNames());
517+
}
519518

520519
/**
521520
* Returns scrolled page for given query
@@ -564,4 +563,15 @@ default void bulkUpdate(List<UpdateQuery> queries) {
564563
* @return Converter in use
565564
*/
566565
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+
567577
}

0 commit comments

Comments
 (0)