Skip to content

Commit d1d09af

Browse files
committed
DATAES-631 - Consolidate query objects.
1 parent 79d75f8 commit d1d09af

37 files changed

+1222
-1211
lines changed

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

+35
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package org.springframework.data.elasticsearch.core;
22

3+
import org.elasticsearch.index.query.QueryBuilder;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56
import org.springframework.data.elasticsearch.ElasticsearchException;
7+
import org.springframework.data.elasticsearch.annotations.Document;
68
import org.springframework.data.elasticsearch.annotations.Mapping;
79
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
810
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
911
import org.springframework.data.elasticsearch.core.index.MappingBuilder;
12+
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
1013
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
14+
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
15+
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
16+
import org.springframework.util.Assert;
1117
import org.springframework.util.StringUtils;
1218

19+
import static org.springframework.util.StringUtils.*;
20+
1321
/**
1422
* AbstractElasticsearchTemplate
1523
*
@@ -21,6 +29,17 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
2129
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractElasticsearchTemplate.class);
2230

2331
protected ElasticsearchConverter elasticsearchConverter;
32+
protected RequestFactory requestFactory;
33+
34+
public RequestFactory getRequestFactory() {
35+
return requestFactory;
36+
}
37+
38+
protected void initialize(ElasticsearchConverter elasticsearchConverter) {
39+
Assert.notNull(elasticsearchConverter, "elasticsearchConverter must not be null.");
40+
this.elasticsearchConverter = elasticsearchConverter;
41+
this.requestFactory = new RequestFactory(elasticsearchConverter);
42+
}
2443

2544
protected ElasticsearchConverter createElasticsearchConverter() {
2645
MappingElasticsearchConverter mappingElasticsearchConverter = new MappingElasticsearchConverter(
@@ -57,4 +76,20 @@ protected String buildMapping(Class<?> clazz) {
5776
public ElasticsearchConverter getElasticsearchConverter() {
5877
return elasticsearchConverter;
5978
}
79+
80+
@Override
81+
public ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz) {
82+
Assert.isTrue(clazz.isAnnotationPresent(Document.class), "Unable to identify index name. " + clazz.getSimpleName()
83+
+ " is not a Document. Make sure the document class is annotated with @Document(indexName=\"foo\")");
84+
return elasticsearchConverter.getMappingContext().getRequiredPersistentEntity(clazz);
85+
}
86+
87+
@Override
88+
public void delete(CriteriaQuery criteriaQuery, IndexCoordinates index) {
89+
QueryBuilder elasticsearchQuery = new CriteriaQueryProcessor().createQueryFromCriteria(criteriaQuery.getCriteria());
90+
Assert.notNull(elasticsearchQuery, "Query can not be null.");
91+
DeleteQuery deleteQuery = new DeleteQuery();
92+
deleteQuery.setQuery(elasticsearchQuery);
93+
delete(deleteQuery, index);
94+
}
6095
}

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

+79-89
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ public interface ElasticsearchOperations {
4444
* adding new alias
4545
*
4646
* @param query
47+
* @param index
4748
* @return
4849
*/
49-
boolean addAlias(AliasQuery query);
50+
boolean addAlias(AliasQuery query, IndexCoordinates index);
5051

5152
/**
5253
* removing previously created alias
5354
*
5455
* @param query
56+
* @param index
5557
* @return
5658
*/
57-
boolean removeAlias(AliasQuery query);
59+
boolean removeAlias(AliasQuery query, IndexCoordinates index);
5860

5961
/**
6062
* Create an index for a class
@@ -126,17 +128,17 @@ public interface ElasticsearchOperations {
126128
* Get mapping for a class
127129
*
128130
* @param clazz
129-
* @param <T>
130131
*/
131-
<T> Map<String, Object> getMapping(Class<T> clazz);
132+
default Map<String, Object> getMapping(Class<?> clazz) {
133+
return getMapping(getIndexCoordinatesFor(clazz));
134+
}
132135

133136
/**
134-
* Get mapping for a given indexName and type
137+
* Get mapping for a given index coordinates
135138
*
136-
* @param indexName
137-
* @param type
139+
* @param index
138140
*/
139-
Map<String, Object> getMapping(String indexName, String type);
141+
Map<String, Object> getMapping(IndexCoordinates index);
140142

141143
/**
142144
* Get settings for a given indexName
@@ -163,13 +165,14 @@ public interface ElasticsearchOperations {
163165
<T> T query(NativeSearchQuery query, ResultsExtractor<T> resultsExtractor);
164166

165167
/**
166-
* Execute the query against elasticsearch and return the first returned object
168+
* Retrieves an object from an index
167169
*
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
171174
*/
172-
<T> T queryForObject(GetQuery query, Class<T> clazz);
175+
<T> T get(GetQuery query, Class<T> clazz, IndexCoordinates index);
173176

174177
/**
175178
* Execute the query against elasticsearch and return the first returned object
@@ -178,7 +181,10 @@ public interface ElasticsearchOperations {
178181
* @param clazz
179182
* @return the first matching object
180183
*/
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+
}
182188

183189
/**
184190
* Execute the query against elasticsearch and return the first returned object
@@ -187,7 +193,10 @@ public interface ElasticsearchOperations {
187193
* @param clazz
188194
* @return the first matching object
189195
*/
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+
}
191200

192201
/**
193202
* Execute the query against elasticsearch and return result as {@link Page}
@@ -326,36 +335,23 @@ default List<List<?>> queryForList(List<NativeSearchQuery> queries, List<Class<?
326335
/**
327336
* return number of elements found by given query
328337
*
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
332341
*/
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+
}
351345

352346
/**
353347
* return number of elements found by given query
354348
*
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
357353
*/
358-
<T> long count(NativeSearchQuery query);
354+
long count(Query query, @Nullable Class<?> clazz, IndexCoordinates index);
359355

360356
/**
361357
* Execute a multiGet against elasticsearch for the given ids
@@ -372,23 +368,23 @@ default List<List<?>> queryForList(List<NativeSearchQuery> queries, List<Class<?
372368
* @param query
373369
* @return returns the document id
374370
*/
375-
String index(IndexQuery query);
371+
String index(IndexQuery query, IndexCoordinates index);
376372

377373
/**
378374
* Partial update of the document
379375
*
380376
* @param updateQuery
381377
* @return
382378
*/
383-
UpdateResponse update(UpdateQuery updateQuery);
379+
UpdateResponse update(UpdateQuery updateQuery, IndexCoordinates index);
384380

385381
/**
386382
* Bulk index all objects. Will do save or update.
387383
*
388384
* @param queries the queries to execute in bulk
389385
*/
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);
392388
}
393389

394390
/**
@@ -398,15 +394,15 @@ default void bulkIndex(List<IndexQuery> queries) {
398394
* @param bulkOptions options to be added to the bulk request
399395
* @since 3.2
400396
*/
401-
void bulkIndex(List<IndexQuery> queries, BulkOptions bulkOptions);
397+
void bulkIndex(List<IndexQuery> queries, BulkOptions bulkOptions, IndexCoordinates index);
402398

403399
/**
404400
* Bulk update all objects. Will do update
405401
*
406402
* @param queries the queries to execute in bulk
407403
*/
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);
410406
}
411407

412408
/**
@@ -416,58 +412,42 @@ default void bulkUpdate(List<UpdateQuery> queries) {
416412
* @param bulkOptions options to be added to the bulk request
417413
* @since 3.2
418414
*/
419-
void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions);
415+
void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions, IndexCoordinates index);
420416

421417
/**
422-
* Delete the one object with provided id
418+
* Delete the one object with provided id.
423419
*
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
427422
* @return documentId of the document deleted
428423
*/
429-
String delete(String indexName, String type, String id);
424+
String delete(String id, IndexCoordinates index);
430425

431426
/**
432427
* Delete all records matching the criteria
433428
*
434-
* @param clazz
435429
* @param criteriaQuery
430+
* @param index
436431
*/
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);
455433

456434
/**
457435
* Delete all records matching the query
458436
*
459437
* @param query
438+
* @param index the index where to delete the records
460439
*/
461-
void delete(DeleteQuery query);
440+
void delete(DeleteQuery query, IndexCoordinates index);
462441

463442
/**
464443
* Deletes an index for given entity
465444
*
466445
* @param clazz
467-
* @param <T>
468446
* @return
469447
*/
470-
<T> boolean deleteIndex(Class<T> clazz);
448+
default boolean deleteIndex(Class<?> clazz) {
449+
return deleteIndex(getPersistentEntityFor(clazz).getIndexName());
450+
}
471451

472452
/**
473453
* Deletes an index for given indexName
@@ -481,10 +461,11 @@ default void bulkUpdate(List<UpdateQuery> queries) {
481461
* check if index is exists
482462
*
483463
* @param clazz
484-
* @param <T>
485464
* @return
486465
*/
487-
<T> boolean indexExists(Class<T> clazz);
466+
default boolean indexExists(Class<?> clazz) {
467+
return indexExists(getIndexCoordinatesFor(clazz).getIndexName());
468+
}
488469

489470
/**
490471
* check if index is exists for given IndexName
@@ -495,27 +476,20 @@ default void bulkUpdate(List<UpdateQuery> queries) {
495476
boolean indexExists(String indexName);
496477

497478
/**
498-
* check if type is exists in an index
479+
* refresh the index(es)
499480
*
500-
* @param index
501-
* @param type
502-
* @return
481+
* @param indexNames
503482
*/
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);
512484

513485
/**
514486
* refresh the index
515487
*
516488
* @param clazz
517489
*/
518-
<T> void refresh(Class<T> clazz);
490+
default <T> void refresh(Class<T> clazz) {
491+
refresh(getIndexCoordinatesFor(clazz).getIndexNames());
492+
}
519493

520494
/**
521495
* Returns scrolled page for given query
@@ -564,4 +538,20 @@ default void bulkUpdate(List<UpdateQuery> queries) {
564538
* @return Converter in use
565539
*/
566540
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+
567557
}

0 commit comments

Comments
 (0)