From 35e9b691bd4057bbc81c2c6f3bfbbedd404183aa Mon Sep 17 00:00:00 2001 From: Peter-Josef Meisch Date: Sat, 30 Nov 2019 15:49:14 +0100 Subject: [PATCH] DATAES-697_-_Query-refactoring-cleanup. Original PR: #347 --- .../core/AbstractElasticsearchTemplate.java | 41 +--- .../core/ElasticsearchOperations.java | 5 - .../core/ReactiveElasticsearchOperations.java | 229 ++++-------------- .../core/ReactiveElasticsearchTemplate.java | 203 ++++------------ .../elasticsearch/core/RequestFactory.java | 39 ++- .../core/query/AbstractQuery.java | 22 -- .../core/query/MoreLikeThisQuery.java | 21 +- .../core/query/NativeSearchQueryBuilder.java | 21 +- .../data/elasticsearch/core/query/Query.java | 32 --- ...tReactiveElasticsearchRepositoryQuery.java | 19 +- .../ReactiveElasticsearchQueryExecution.java | 39 ++- ...SimpleReactiveElasticsearchRepository.java | 31 +-- .../core/ElasticsearchTemplateTests.java | 96 +++----- .../ReactiveElasticsearchTemplateTests.java | 63 ++--- ...eactiveElasticsearchTemplateUnitTests.java | 25 +- ...ElasticsearchTemplateAggregationTests.java | 3 +- .../repositories/spel/SpELEntityTests.java | 3 - 17 files changed, 265 insertions(+), 627 deletions(-) diff --git a/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java index 83ee37678..806d52842 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java @@ -34,7 +34,6 @@ import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersiste import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; import org.springframework.data.elasticsearch.core.query.DeleteQuery; import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery; -import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.util.Assert; @@ -53,6 +52,9 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper protected ElasticsearchConverter elasticsearchConverter; protected RequestFactory requestFactory; + /** + * @since 4.0 + */ public RequestFactory getRequestFactory() { return requestFactory; } @@ -197,43 +199,6 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper } } - /** - * @param query - * @param clazz - * @deprecated index names and types should not be set in query - */ - @Deprecated - protected void setPersistentEntityIndexAndType(Query query, Class clazz) { - if (query.getIndices().isEmpty()) { - String[] indices = retrieveIndexNameFromPersistentEntity(clazz); - - if (indices != null) { - query.addIndices(indices); - } - } - if (query.getTypes().isEmpty()) { - String[] types = retrieveTypeFromPersistentEntity(clazz); - - if (types != null) { - query.addTypes(types); - } - } - } - - private String[] retrieveIndexNameFromPersistentEntity(Class clazz) { - if (clazz != null) { - return new String[] { getPersistentEntityFor(clazz).getIndexName() }; - } - return null; - } - - private String[] retrieveTypeFromPersistentEntity(Class clazz) { - if (clazz != null) { - return new String[] { getPersistentEntityFor(clazz).getIndexType() }; - } - return null; - } - @Override public List> queryForPage(List queries, Class clazz, IndexCoordinates index) { MultiSearchRequest request = new MultiSearchRequest(); diff --git a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java index 88625292a..ef467a408 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java @@ -483,11 +483,6 @@ public interface ElasticsearchOperations { */ ElasticsearchConverter getElasticsearchConverter(); - /** - * @since 4.0 - */ - RequestFactory getRequestFactory(); - /** * @param clazz * @return the IndexCoordinates defined on the entity. diff --git a/src/main/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchOperations.java b/src/main/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchOperations.java index 617e172c7..947458f49 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchOperations.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchOperations.java @@ -23,6 +23,7 @@ import org.reactivestreams.Publisher; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.data.elasticsearch.core.query.StringQuery; import org.springframework.lang.Nullable; @@ -72,35 +73,7 @@ public interface ReactiveElasticsearchOperations { * @return a {@link Mono} emitting the saved entity. */ default Mono save(T entity) { - return save(entity, null); - } - - /** - * Index the entity, once available, in the given {@literal index}. If the index is {@literal null} or empty the index - * name provided via entity metadata is used. - * - * @param entityPublisher must not be {@literal null}. - * @param index the name of the target index. Can be {@literal null}. - * @param - * @return a {@link Mono} emitting the saved entity. - */ - default Mono save(Mono entityPublisher, String index) { - - Assert.notNull(entityPublisher, "EntityPublisher must not be null!"); - return entityPublisher.flatMap(it -> save(it, index)); - } - - /** - * Index the entity in the given {@literal index}. If the index is {@literal null} or empty the index name provided - * via entity metadata is used. - * - * @param entity must not be {@literal null}. - * @param index the name of the target index. Can be {@literal null}. - * @param - * @return a {@link Mono} emitting the saved entity. - */ - default Mono save(T entity, @Nullable String index) { - return save(entity, index, null); + return save(entity, getIndexCoordinatesFor(entity.getClass())); } /** @@ -109,15 +82,14 @@ public interface ReactiveElasticsearchOperations { * {@literal type}. * * @param entityPublisher must not be {@literal null}. - * @param index the name of the target index. Can be {@literal null}. - * @param type the name of the type within the index. Can be {@literal null}. + * @param index the target index, must not be {@literal null} * @param * @return a {@link Mono} emitting the saved entity. */ - default Mono save(Mono entityPublisher, @Nullable String index, @Nullable String type) { + default Mono save(Mono entityPublisher, IndexCoordinates index) { Assert.notNull(entityPublisher, "EntityPublisher must not be null!"); - return entityPublisher.flatMap(it -> save(it, index, type)); + return entityPublisher.flatMap(it -> save(it, index)); } /** @@ -125,12 +97,11 @@ public interface ReactiveElasticsearchOperations { * {@literal null} or empty the index name provided via entity metadata is used. Same for the {@literal type}. * * @param entity must not be {@literal null}. - * @param index the name of the target index. Can be {@literal null}. - * @param type the name of the type within the index. Can be {@literal null}. + * @param index the target index, must not be {@literal null} * @param * @return a {@link Mono} emitting the saved entity. */ - Mono save(T entity, @Nullable String index, @Nullable String type); + Mono save(T entity, IndexCoordinates index); /** * Find the document with the given {@literal id} mapped onto the given {@literal entityType}. @@ -141,35 +112,18 @@ public interface ReactiveElasticsearchOperations { * @return {@link Mono#empty()} if not found. */ default Mono findById(String id, Class entityType) { - return findById(id, entityType, null); - } - - /** - * Fetch the entity with given {@literal id}. - * - * @param id the {@literal _id} of the document to fetch. - * @param entityType the domain type used for mapping the document. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @param - * @return {@link Mono#empty()} if not found. - */ - default Mono findById(String id, Class entityType, @Nullable String index) { - return findById(id, entityType, index, null); + return findById(id, entityType, getIndexCoordinatesFor(entityType)); } /** * Fetch the entity with given {@literal id}. * * @param id must not be {@literal null}. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @param type the name of the target type. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. + * @param index the target index, must not be {@literal null} * @param * @return the {@link Mono} emitting the entity or signalling completion if none found. */ - Mono findById(String id, Class entityType, @Nullable String index, @Nullable String type); + Mono findById(String id, Class entityType, IndexCoordinates index); /** * Check if an entity with given {@literal id} exists. @@ -179,33 +133,17 @@ public interface ReactiveElasticsearchOperations { * @return a {@link Mono} emitting {@literal true} if a matching document exists, {@literal false} otherwise. */ default Mono exists(String id, Class entityType) { - return exists(id, entityType, null); + return exists(id, entityType, getIndexCoordinatesFor(entityType)); } /** * Check if an entity with given {@literal id} exists. * * @param id the {@literal _id} of the document to look for. - * @param entityType the domain type used. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. + * @param index the target index, must not be {@literal null} * @return a {@link Mono} emitting {@literal true} if a matching document exists, {@literal false} otherwise. */ - default Mono exists(String id, Class entityType, @Nullable String index) { - return exists(id, entityType, index, null); - } - - /** - * Check if an entity with given {@literal id} exists. - * - * @param id the {@literal _id} of the document to look for. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @param type the name of the target type. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @return a {@link Mono} emitting {@literal true} if a matching document exists, {@literal false} otherwise. - */ - Mono exists(String id, Class entityType, @Nullable String index, @Nullable String type); + Mono exists(String id, Class entityType, IndexCoordinates index); /** * Search the index for entities matching the given {@link Query query}.
@@ -235,7 +173,7 @@ public interface ReactiveElasticsearchOperations { * @return a {@link Flux} emitting matching entities one by one. */ default Flux find(Query query, Class entityType, Class returnType) { - return find(query, entityType, null, null, returnType); + return find(query, entityType, returnType, getIndexCoordinatesFor(entityType)); } /** @@ -243,27 +181,12 @@ public interface ReactiveElasticsearchOperations { * * @param query must not be {@literal null}. * @param entityType must not be {@literal null}. - * @param - * @return a {@link Flux} emitting matching entities one by one. - */ - default Flux find(Query query, Class entityType, @Nullable String index) { - return find(query, entityType, index, null); - } - - /** - * Search the index for entities matching the given {@link Query query}. - * - * @param query must not be {@literal null}. - * @param entityType must not be {@literal null}. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @param type the name of the target type. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. + * @param index the target index, must not be {@literal null} * @param * @returnm a {@link Flux} emitting matching entities one by one. */ - default Flux find(Query query, Class entityType, @Nullable String index, @Nullable String type) { - return find(query, entityType, index, type, entityType); + default Flux find(Query query, Class entityType, IndexCoordinates index) { + return find(query, entityType, entityType, index); } /** @@ -271,16 +194,12 @@ public interface ReactiveElasticsearchOperations { * * @param query must not be {@literal null}. * @param entityType must not be {@literal null}. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @param type the name of the target type. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. * @param resultType the projection result type. + * @param index the target index, must not be {@literal null} * @param * @return a {@link Flux} emitting matching entities one by one. */ - Flux find(Query query, Class entityType, @Nullable String index, @Nullable String type, - Class resultType); + Flux find(Query query, Class entityType, Class resultType, IndexCoordinates index); /** * Count the number of documents matching the given {@link Query}. @@ -289,7 +208,7 @@ public interface ReactiveElasticsearchOperations { * @return a {@link Mono} emitting the nr of matching documents. */ default Mono count(Class entityType) { - return count(new StringQuery(QueryBuilders.matchAllQuery().toString()), entityType, null); + return count(new StringQuery(QueryBuilders.matchAllQuery().toString()), entityType); } /** @@ -300,7 +219,7 @@ public interface ReactiveElasticsearchOperations { * @return a {@link Mono} emitting the nr of matching documents. */ default Mono count(Query query, Class entityType) { - return count(query, entityType, null); + return count(query, entityType, getIndexCoordinatesFor(entityType)); } /** @@ -308,26 +227,10 @@ public interface ReactiveElasticsearchOperations { * * @param query must not be {@literal null}. * @param entityType must not be {@literal null}. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. + * @param index the target index, must not be {@literal null} * @return a {@link Mono} emitting the nr of matching documents. */ - default Mono count(Query query, Class entityType, @Nullable String index) { - return count(query, entityType, index, null); - } - - /** - * Count the number of documents matching the given {@link Query}. - * - * @param query must not be {@literal null}. - * @param entityType must not be {@literal null}. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @param type the name of the target type. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @return a {@link Mono} emitting the nr of matching documents. - */ - Mono count(Query query, Class entityType, @Nullable String index, @Nullable String type); + Mono count(Query query, Class entityType, IndexCoordinates index); /** * Delete the given entity extracting index and type from entity metadata. @@ -336,47 +239,30 @@ public interface ReactiveElasticsearchOperations { * @return a {@link Mono} emitting the {@literal id} of the removed document. */ default Mono delete(Object entity) { - return delete(entity, null); + return delete(entity, getIndexCoordinatesFor(entity.getClass())); } /** * Delete the given entity extracting index and type from entity metadata. * * @param entity must not be {@literal null}. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. + * @param index the target index, must not be {@literal null} * @return a {@link Mono} emitting the {@literal id} of the removed document. */ - default Mono delete(Object entity, @Nullable String index) { - return delete(entity, index, null); - } - - /** - * Delete the given entity extracting index and type from entity metadata. - * - * @param entity must not be {@literal null}. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @param type the name of the target type. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @return a {@link Mono} emitting the {@literal id} of the removed document. - */ - Mono delete(Object entity, @Nullable String index, @Nullable String type); + Mono delete(Object entity, IndexCoordinates index); /** * Delete the entity with given {@literal id}. * * @param id must not be {@literal null}. - * @param index the name of the target index. - * @param type the name of the target type. + * @param index the target index, must not be {@literal null} * @return a {@link Mono} emitting the {@literal id} of the removed document. */ - default Mono deleteById(String id, String index, String type) { + default Mono deleteById(String id, IndexCoordinates index) { Assert.notNull(index, "Index must not be null!"); - Assert.notNull(type, "Type must not be null!"); - return deleteById(id, Object.class, index, type); + return deleteById(id, Object.class, index); } /** @@ -387,7 +273,7 @@ public interface ReactiveElasticsearchOperations { * @return a {@link Mono} emitting the {@literal id} of the removed document. */ default Mono deleteById(String id, Class entityType) { - return deleteById(id, entityType, null); + return deleteById(id, entityType, getIndexCoordinatesFor(entityType)); } /** @@ -395,26 +281,10 @@ public interface ReactiveElasticsearchOperations { * * @param id must not be {@literal null}. * @param entityType must not be {@literal null}. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. + * @param index the target index, must not be {@literal null} * @return a {@link Mono} emitting the {@literal id} of the removed document. */ - default Mono deleteById(String id, Class entityType, @Nullable String index) { - return deleteById(id, entityType, index, null); - } - - /** - * Delete the entity with given {@literal id} extracting index and type from entity metadata. - * - * @param id must not be {@literal null}. - * @param entityType must not be {@literal null}. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @param type the name of the target type. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @return a {@link Mono} emitting the {@literal id} of the removed document. - */ - Mono deleteById(String id, Class entityType, @Nullable String index, @Nullable String type); + Mono deleteById(String id, Class entityType, IndexCoordinates index); /** * Delete the documents matching the given {@link Query} extracting index and type from entity metadata. @@ -424,7 +294,7 @@ public interface ReactiveElasticsearchOperations { * @return a {@link Mono} emitting the number of the removed documents. */ default Mono deleteBy(Query query, Class entityType) { - return deleteBy(query, entityType, null); + return deleteBy(query, entityType, getIndexCoordinatesFor(entityType)); } /** @@ -432,26 +302,10 @@ public interface ReactiveElasticsearchOperations { * * @param query must not be {@literal null}. * @param entityType must not be {@literal null}. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. + * @param index the target index, must not be {@literal null} * @return a {@link Mono} emitting the number of the removed documents. */ - default Mono deleteBy(Query query, Class entityType, @Nullable String index) { - return deleteBy(query, entityType, index, null); - } - - /** - * Delete the documents matching the given {@link Query} extracting index and type from entity metadata. - * - * @param query must not be {@literal null}. - * @param entityType must not be {@literal null}. - * @param index the name of the target index. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @param type the name of the target type. Overwrites document metadata from {@literal entityType} if not - * {@literal null}. - * @return a {@link Mono} emitting the number of the removed documents. - */ - Mono deleteBy(Query query, Class entityType, @Nullable String index, @Nullable String type); + Mono deleteBy(Query query, Class entityType, IndexCoordinates index); /** * Get the {@link ElasticsearchConverter} used. @@ -460,6 +314,19 @@ public interface ReactiveElasticsearchOperations { */ ElasticsearchConverter getElasticsearchConverter(); + @Nullable + ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz); + + /** + * @param clazz + * @return the IndexCoordinates defined on the entity. + * @since 4.0 + */ + default IndexCoordinates getIndexCoordinatesFor(Class clazz) { + ElasticsearchPersistentEntity entity = getPersistentEntityFor(clazz); + return IndexCoordinates.of(entity.getIndexName()).withTypes(entity.getIndexType()); + } + /** * Callback interface to be used with {@link #execute(ClientCallback)} for operating directly on * {@link ReactiveElasticsearchClient}. diff --git a/src/main/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplate.java index f837f014f..92ced2299 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplate.java @@ -17,14 +17,12 @@ package org.springframework.data.elasticsearch.core; import static org.elasticsearch.index.VersionType.*; -import lombok.NonNull; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.function.Supplier; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.get.GetRequest; @@ -87,9 +85,10 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera private final ReactiveElasticsearchClient client; private final ElasticsearchConverter converter; - private final @NonNull MappingContext, ElasticsearchPersistentProperty> mappingContext; + private final MappingContext, ElasticsearchPersistentProperty> mappingContext; private final ElasticsearchExceptionTranslator exceptionTranslator; private final EntityOperations operations; + protected RequestFactory requestFactory; private @Nullable RefreshPolicy refreshPolicy = RefreshPolicy.IMMEDIATE; private @Nullable IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosedIgnoreThrottled(); @@ -99,6 +98,8 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera } public ReactiveElasticsearchTemplate(ReactiveElasticsearchClient client, ElasticsearchConverter converter) { + Assert.notNull(client, "client must not be null"); + Assert.notNull(converter, "converter must not be null"); this.client = client; this.converter = converter; @@ -106,6 +107,7 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera this.exceptionTranslator = new ElasticsearchExceptionTranslator(); this.operations = new EntityOperations(this.mappingContext); + this.requestFactory = new RequestFactory(converter); } /* @@ -119,33 +121,30 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera /* * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#index(Object, String, String) + * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#index(Object, IndexCoordinates) */ @Override - public Mono save(T entity, @Nullable String index, @Nullable String type) { + public Mono save(T entity, IndexCoordinates index) { Assert.notNull(entity, "Entity must not be null!"); AdaptibleEntity adaptableEntity = operations.forEntity(entity, converter.getConversionService()); - return doIndex(entity, adaptableEntity, index, type) // + return doIndex(entity, adaptableEntity, index) // .map(it -> { return adaptableEntity.populateIdIfNecessary(it.getId()); }); } - private Mono doIndex(Object value, AdaptibleEntity entity, @Nullable String index, - @Nullable String type) { + private Mono doIndex(Object value, AdaptibleEntity entity, IndexCoordinates index) { return Mono.defer(() -> { Object id = entity.getId(); - IndexCoordinates indexCoordinates = operations.determineIndex(entity, index, type); - IndexRequest request = id != null - ? new IndexRequest(indexCoordinates.getIndexName(), indexCoordinates.getTypeName(), converter.convertId(id)) - : new IndexRequest(indexCoordinates.getIndexName(), indexCoordinates.getTypeName()); + ? new IndexRequest(index.getIndexName(), index.getTypeName(), converter.convertId(id)) + : new IndexRequest(index.getIndexName(), index.getTypeName()); request.source(converter.mapObject(value).toJson(), Requests.INDEX_CONTENT_TYPE); @@ -165,69 +164,60 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera /* * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#findById(String, Class, String, String) + * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#findById(String, Class, IndexCoordinates) */ @Override - public Mono findById(String id, Class entityType, @Nullable String index, @Nullable String type) { + public Mono findById(String id, Class entityType, IndexCoordinates index) { Assert.notNull(id, "Id must not be null!"); - return doFindById(id, getPersistentEntity(entityType), index, type) + return doFindById(id, getPersistentEntityFor(entityType), index) .map(it -> converter.mapDocument(DocumentAdapters.from(it), entityType)); } - private Mono doFindById(String id, ElasticsearchPersistentEntity entity, @Nullable String index, - @Nullable String type) { + private Mono doFindById(String id, ElasticsearchPersistentEntity entity, IndexCoordinates index) { return Mono.defer(() -> { - IndexCoordinates indexCoordinates = operations.determineIndex(entity, index, type); - - return doFindById(new GetRequest(indexCoordinates.getIndexName(), indexCoordinates.getTypeName(), id)); + return doFindById(new GetRequest(index.getIndexName(), index.getTypeName(), id)); }); } /* * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#exists(String, Class, String, String) - * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#exists(String, Class, String, String) + * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#exists(String, Class, IndexCoordinates) */ @Override - public Mono exists(String id, Class entityType, String index, String type) { + public Mono exists(String id, Class entityType, IndexCoordinates index) { Assert.notNull(id, "Id must not be null!"); - return doExists(id, getPersistentEntity(entityType), index, type); + return doExists(id, getPersistentEntityFor(entityType), index); } - private Mono doExists(String id, ElasticsearchPersistentEntity entity, @Nullable String index, - @Nullable String type) { + private Mono doExists(String id, ElasticsearchPersistentEntity entity, @Nullable IndexCoordinates index) { - return Mono.defer(() -> { - - IndexCoordinates indexCoordinates = operations.determineIndex(entity, index, type); - - return doExists(new GetRequest(indexCoordinates.getIndexName(), indexCoordinates.getTypeName(), id)); - }); + return Mono.defer(() -> doExists(new GetRequest(index.getIndexName(), index.getTypeName(), id))); } /* * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#find(Query, Class, String, String, Class) + * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#find(Query, Class, Class, IndexCoordinates) */ @Override - public Flux find(Query query, Class entityType, @Nullable String index, @Nullable String type, - Class resultType) { + public Flux find(Query query, Class entityType, Class resultType, IndexCoordinates index) { - return doFind(query, getPersistentEntity(entityType), index, type) - .map(it -> converter.mapDocument(DocumentAdapters.from(it), resultType)); + return doFind(query, entityType, index).map(it -> converter.mapDocument(DocumentAdapters.from(it), resultType)); } - private Flux doFind(Query query, ElasticsearchPersistentEntity entity, @Nullable String index, - @Nullable String type) { + private Flux doFind(Query query, Class clazz, IndexCoordinates index) { return Flux.defer(() -> { - SearchRequest request = prepareSearchRequest(buildSearchRequest(query, entity, index, type)); + SearchRequest request = requestFactory.searchRequest(query, clazz, index); + + if (indicesOptions != null) { + request.indicesOptions(indicesOptions); + } if (query.getPageable().isPaged() || query.isLimiting()) { return doFind(request); @@ -238,26 +228,23 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera } @Override - public Mono count(Query query, Class entityType, String index, String type) { - return doCount(query, getPersistentEntity(entityType), index, type); + public Mono count(Query query, Class entityType, IndexCoordinates index) { + return doCount(query, getPersistentEntityFor(entityType), index); } - private Mono doCount(Query query, ElasticsearchPersistentEntity entity, @Nullable String index, - @Nullable String type) { + private Mono doCount(Query query, ElasticsearchPersistentEntity entity, IndexCoordinates index) { return Mono.defer(() -> { - CountRequest countRequest = buildCountRequest(query, entity, index, type); + CountRequest countRequest = buildCountRequest(query, entity, index); CountRequest request = prepareCountRequest(countRequest); return doCount(request); }); } - private CountRequest buildCountRequest(Query query, ElasticsearchPersistentEntity entity, @Nullable String index, - @Nullable String type) { + private CountRequest buildCountRequest(Query query, ElasticsearchPersistentEntity entity, IndexCoordinates index) { - IndexCoordinates indexCoordinates = operations.determineIndex(entity, index, type); - CountRequest request = new CountRequest(indices(query, indexCoordinates::getIndexName)); + CountRequest request = new CountRequest(index.getIndexNames()); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(mappedQuery(query, entity)); searchSourceBuilder.trackScores(query.getTrackScores()); @@ -292,131 +279,60 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera return request; } - private SearchRequest buildSearchRequest(Query query, ElasticsearchPersistentEntity entity, @Nullable String index, - @Nullable String type) { - IndexCoordinates indexCoordinates = operations.determineIndex(entity, index, type); - SearchRequest request = new SearchRequest(indices(query, indexCoordinates::getIndexName)); - SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); - searchSourceBuilder.query(mappedQuery(query, entity)); - searchSourceBuilder.version(entity.hasVersionProperty()); - searchSourceBuilder.trackScores(query.getTrackScores()); - - QueryBuilder postFilterQuery = mappedFilterQuery(query, entity); - if (postFilterQuery != null) { - searchSourceBuilder.postFilter(postFilterQuery); - } - - if (query.getSourceFilter() != null) { - searchSourceBuilder.fetchSource(query.getSourceFilter().getIncludes(), query.getSourceFilter().getExcludes()); - } - - if (query instanceof NativeSearchQuery && ((NativeSearchQuery) query).getCollapseBuilder() != null) { - searchSourceBuilder.collapse(((NativeSearchQuery) query).getCollapseBuilder()); - } - - sort(query, entity).forEach(searchSourceBuilder::sort); - - if (query.getMinScore() > 0) { - searchSourceBuilder.minScore(query.getMinScore()); - } - - if (query.getIndicesOptions() != null) { - request.indicesOptions(query.getIndicesOptions()); - } - - if (query.getPreference() != null) { - request.preference(query.getPreference()); - } - - if (query.getSearchType() != null) { - request.searchType(query.getSearchType()); - } - - Pageable pageable = query.getPageable(); - - if (pageable.isPaged()) { - - long offset = pageable.getOffset(); - if (offset > Integer.MAX_VALUE) { - throw new IllegalArgumentException(String.format("Offset must not be more than %s", Integer.MAX_VALUE)); - } - - searchSourceBuilder.from((int) offset); - searchSourceBuilder.size(pageable.getPageSize()); - - request.source(searchSourceBuilder); - request.source(searchSourceBuilder); - } else if (query.isLimiting()) { - searchSourceBuilder.from(0); - searchSourceBuilder.size(query.getMaxResults()); - - request.source(searchSourceBuilder); - } else { - request.source(searchSourceBuilder); - } - return request; - } - /* * (non-Javadoc) * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#delete(Object, String, String) */ @Override - public Mono delete(Object entity, @Nullable String index, @Nullable String type) { + public Mono delete(Object entity, IndexCoordinates index) { Entity elasticsearchEntity = operations.forEntity(entity); return Mono.defer(() -> doDeleteById(entity, converter.convertId(elasticsearchEntity.getId()), - elasticsearchEntity.getPersistentEntity(), index, type)); + elasticsearchEntity.getPersistentEntity(), index)); } /* * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#delete(String, Class, String, String) + * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#delete(String, Class, IndexCoordinates) */ @Override - public Mono deleteById(String id, Class entityType, @Nullable String index, @Nullable String type) { + public Mono deleteById(String id, Class entityType, IndexCoordinates index) { Assert.notNull(id, "Id must not be null!"); - return doDeleteById(null, id, getPersistentEntity(entityType), index, type); + return doDeleteById(null, id, getPersistentEntityFor(entityType), index); } private Mono doDeleteById(@Nullable Object source, String id, ElasticsearchPersistentEntity entity, - @Nullable String index, @Nullable String type) { + IndexCoordinates index) { return Mono.defer(() -> { - IndexCoordinates indexCoordinates = operations.determineIndex(entity, index, type); - - return doDelete(prepareDeleteRequest(source, - new DeleteRequest(indexCoordinates.getIndexName(), indexCoordinates.getTypeName(), id))); + return doDelete(prepareDeleteRequest(source, new DeleteRequest(index.getIndexName(), index.getTypeName(), id))); }); } /* * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#deleteBy(Query, Class, String, String) + * @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#deleteBy(Query, Class, IndexCoordinates) */ @Override - public Mono deleteBy(Query query, Class entityType, String index, String type) { + public Mono deleteBy(Query query, Class entityType, IndexCoordinates index) { Assert.notNull(query, "Query must not be null!"); - return doDeleteBy(query, getPersistentEntity(entityType), index, type).map(BulkByScrollResponse::getDeleted) + return doDeleteBy(query, getPersistentEntityFor(entityType), index).map(BulkByScrollResponse::getDeleted) .publishNext(); } private Flux doDeleteBy(Query query, ElasticsearchPersistentEntity entity, - @Nullable String index, @Nullable String type) { + IndexCoordinates index) { return Flux.defer(() -> { - - IndexCoordinates indexCoordinates = operations.determineIndex(entity, index, type); - - DeleteByQueryRequest request = new DeleteByQueryRequest(indices(query, indexCoordinates::getIndexName)); - request.types(indexTypes(query, indexCoordinates::getTypeName)); + DeleteByQueryRequest request = new DeleteByQueryRequest(index.getIndexNames()); + request.types(index.getTypeNames()); request.setQuery(mappedQuery(query, entity)); return doDeleteBy(prepareDeleteByRequest(request)); @@ -675,24 +591,6 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera // private helpers - private static String[] indices(Query query, Supplier index) { - - if (query.getIndices().isEmpty()) { - return new String[] { index.get() }; - } - - return query.getIndices().toArray(new String[0]); - } - - private static String[] indexTypes(Query query, Supplier indexType) { - - if (query.getTypes().isEmpty()) { - return new String[] { indexType.get() }; - } - - return query.getTypes().toArray(new String[0]); - } - private static List sort(Query query, ElasticsearchPersistentEntity entity) { if (query.getSort() == null || query.getSort().isUnsorted()) { @@ -749,8 +647,9 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera return null; } + @Override @Nullable - private ElasticsearchPersistentEntity getPersistentEntity(@Nullable Class type) { + public ElasticsearchPersistentEntity getPersistentEntityFor(@Nullable Class type) { return type != null ? mappingContext.getPersistentEntity(type) : null; } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/RequestFactory.java b/src/main/java/org/springframework/data/elasticsearch/core/RequestFactory.java index 00e94d0a1..e1b880015 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/RequestFactory.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/RequestFactory.java @@ -604,17 +604,7 @@ class RequestFactory { } if (query instanceof NativeSearchQuery) { - NativeSearchQuery nativeSearchQuery = (NativeSearchQuery) query; - - if (!nativeSearchQuery.getScriptFields().isEmpty()) { - for (ScriptField scriptedField : nativeSearchQuery.getScriptFields()) { - sourceBuilder.scriptField(scriptedField.fieldName(), scriptedField.script()); - } - } - - if (nativeSearchQuery.getCollapseBuilder() != null) { - sourceBuilder.collapse(nativeSearchQuery.getCollapseBuilder()); - } + prepareNativeSearch((NativeSearchQuery) query, sourceBuilder); } @@ -681,6 +671,33 @@ class RequestFactory { return searchRequestBuilder; } + private void prepareNativeSearch(NativeSearchQuery query, SearchSourceBuilder sourceBuilder) { + NativeSearchQuery nativeSearchQuery = query; + + if (!nativeSearchQuery.getScriptFields().isEmpty()) { + for (ScriptField scriptedField : nativeSearchQuery.getScriptFields()) { + sourceBuilder.scriptField(scriptedField.fieldName(), scriptedField.script()); + } + } + + if (nativeSearchQuery.getCollapseBuilder() != null) { + sourceBuilder.collapse(nativeSearchQuery.getCollapseBuilder()); + } + + if (!isEmpty(nativeSearchQuery.getIndicesBoost())) { + for (IndexBoost indexBoost : nativeSearchQuery.getIndicesBoost()) { + sourceBuilder.indexBoost(indexBoost.getIndexName(), indexBoost.getBoost()); + } + } + + if (!isEmpty(nativeSearchQuery.getAggregations())) { + for (AbstractAggregationBuilder aggregationBuilder : nativeSearchQuery.getAggregations()) { + sourceBuilder.aggregation(aggregationBuilder); + } + } + + } + private void prepareNativeSearch(SearchRequestBuilder searchRequestBuilder, NativeSearchQuery nativeSearchQuery) { if (!isEmpty(nativeSearchQuery.getScriptFields())) { for (ScriptField scriptedField : nativeSearchQuery.getScriptFields()) { diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java b/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java index 52968b281..1c2a106d7 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java @@ -42,8 +42,6 @@ abstract class AbstractQuery implements Query { protected Pageable pageable = DEFAULT_PAGE; protected Sort sort; - protected List indices = new ArrayList<>(); - protected List types = new ArrayList<>(); protected List fields = new ArrayList<>(); protected SourceFilter sourceFilter; protected float minScore; @@ -84,26 +82,6 @@ abstract class AbstractQuery implements Query { return fields; } - @Override - public List getIndices() { - return indices; - } - - @Override - public void addIndices(String... indices) { - addAll(this.indices, indices); - } - - @Override - public void addTypes(String... types) { - addAll(this.types, types); - } - - @Override - public List getTypes() { - return types; - } - @Override public void addSourceFilter(SourceFilter sourceFilter) { this.sourceFilter = sourceFilter; diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/MoreLikeThisQuery.java b/src/main/java/org/springframework/data/elasticsearch/core/query/MoreLikeThisQuery.java index 0eb0f5357..f8b9c3aba 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/MoreLikeThisQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/MoreLikeThisQuery.java @@ -15,7 +15,7 @@ */ package org.springframework.data.elasticsearch.core.query; -import static java.util.Collections.addAll; +import static java.util.Collections.*; import static org.springframework.data.elasticsearch.core.query.AbstractQuery.*; import java.util.ArrayList; @@ -28,12 +28,11 @@ import org.springframework.data.domain.Pageable; * * @author Rizwan Idrees * @author Mohsin Husen + * @author Peter-Josef Meisch */ public class MoreLikeThisQuery { private String id; - private String indexName; - private String type; private List searchIndices = new ArrayList<>(); private List searchTypes = new ArrayList<>(); private List fields = new ArrayList<>(); @@ -57,22 +56,6 @@ public class MoreLikeThisQuery { this.id = id; } - public String getIndexName() { - return indexName; - } - - public void setIndexName(String indexName) { - this.indexName = indexName; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - public List getSearchIndices() { return searchIndices; } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java b/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java index 5a8412549..4c682653e 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java @@ -42,6 +42,7 @@ import org.springframework.data.domain.Pageable; * @author Jean-Baptiste Nizet * @author Martin Choraine * @author Farid Azaza + * @author Peter-Josef Meisch */ public class NativeSearchQueryBuilder { @@ -53,8 +54,6 @@ public class NativeSearchQueryBuilder { private HighlightBuilder highlightBuilder; private HighlightBuilder.Field[] highlightFields; private Pageable pageable = Pageable.unpaged(); - private String[] indices; - private String[] types; private String[] fields; private SourceFilter sourceFilter; private CollapseBuilder collapseBuilder; @@ -117,16 +116,6 @@ public class NativeSearchQueryBuilder { return this; } - public NativeSearchQueryBuilder withIndices(String... indices) { - this.indices = indices; - return this; - } - - public NativeSearchQueryBuilder withTypes(String... types) { - this.types = types; - return this; - } - public NativeSearchQueryBuilder withFields(String... fields) { this.fields = fields; return this; @@ -184,14 +173,6 @@ public class NativeSearchQueryBuilder { nativeSearchQuery.setPageable(pageable); nativeSearchQuery.setTrackScores(trackScores); - if (indices != null) { - nativeSearchQuery.addIndices(indices); - } - - if (types != null) { - nativeSearchQuery.addTypes(types); - } - if (fields != null) { nativeSearchQuery.addFields(fields); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java b/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java index 29ca79549..581b6734e 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java @@ -81,38 +81,6 @@ public interface Query { */ Sort getSort(); - /** - * Get Indices to be searched - * - * @return - */ - @Deprecated - List getIndices(); - - /** - * Add Indices to be added as part of search request - * - * @param indices - */ - @Deprecated - void addIndices(String... indices); - - /** - * Add types to be searched - * - * @param types - */ - @Deprecated - void addTypes(String... types); - - /** - * Get types to be searched - * - * @return - */ - @Deprecated - List getTypes(); - /** * Add fields to be added as part of search request * diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/query/AbstractReactiveElasticsearchRepositoryQuery.java b/src/main/java/org/springframework/data/elasticsearch/repository/query/AbstractReactiveElasticsearchRepositoryQuery.java index 345f670b8..397794ff4 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/query/AbstractReactiveElasticsearchRepositoryQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/query/AbstractReactiveElasticsearchRepositoryQuery.java @@ -20,6 +20,7 @@ import reactor.core.publisher.Mono; import org.reactivestreams.Publisher; import org.springframework.core.convert.converter.Converter; +import org.springframework.data.elasticsearch.core.IndexCoordinates; import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; @@ -36,6 +37,7 @@ import org.springframework.data.repository.query.ResultProcessor; * AbstractElasticsearchRepositoryQuery * * @author Christoph Strobl + * @author Peter-Josef Meisch * @since 3.2 */ abstract class AbstractReactiveElasticsearchRepositoryQuery implements RepositoryQuery { @@ -79,14 +81,15 @@ abstract class AbstractReactiveElasticsearchRepositoryQuery implements Repositor Query query = createQuery( new ConvertingParameterAccessor(elasticsearchOperations.getElasticsearchConverter(), parameterAccessor)); - Class typeToRead = processor.getReturnedType().getTypeToRead(); + Class targetType = processor.getReturnedType().getTypeToRead(); String indexName = queryMethod.getEntityInformation().getIndexName(); String indexTypeName = queryMethod.getEntityInformation().getIndexTypeName(); + IndexCoordinates index = IndexCoordinates.of(indexName).withTypes(indexTypeName); ReactiveElasticsearchQueryExecution execution = getExecution(parameterAccessor, new ResultProcessingConverter(processor, elasticsearchOperations)); - return execution.execute(query, processor.getReturnedType().getDomainType(), indexName, indexTypeName, typeToRead); + return execution.execute(query, processor.getReturnedType().getDomainType(), targetType, index); } private ReactiveElasticsearchQueryExecution getExecution(ElasticsearchParameterAccessor accessor, @@ -106,15 +109,17 @@ abstract class AbstractReactiveElasticsearchRepositoryQuery implements Repositor ReactiveElasticsearchOperations operations) { if (isDeleteQuery()) { - return (q, t, i, it, tt) -> operations.deleteBy(q, t, i, it); + return (query, type, targetType, indexCoordinates) -> operations.deleteBy(query, type, indexCoordinates); } else if (isCountQuery()) { - return (q, t, i, it, tt) -> operations.count(q, t, i, it); + return (query, type, targetType, indexCoordinates) -> operations.count(query, type, indexCoordinates); } else if (isExistsQuery()) { - return (q, t, i, it, tt) -> operations.count(q, t, i, it).map(count -> count > 0); + return (query, type, targetType, indexCoordinates) -> operations.count(query, type, indexCoordinates) + .map(count -> count > 0); } else if (queryMethod.isCollectionQuery()) { - return (q, t, i, it, tt) -> operations.find(q.setPageable(accessor.getPageable()), t, i, it, tt); + return (query, type, targetType, indexCoordinates) -> operations.find(query.setPageable(accessor.getPageable()), + type, targetType, indexCoordinates); } else { - return (q, t, i, it, tt) -> operations.find(q, t, i, it, tt); + return (query, type, targetType, indexCoordinates) -> operations.find(query, type, targetType, indexCoordinates); } } diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/query/ReactiveElasticsearchQueryExecution.java b/src/main/java/org/springframework/data/elasticsearch/repository/query/ReactiveElasticsearchQueryExecution.java index 960a43ebf..af02d8080 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/query/ReactiveElasticsearchQueryExecution.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/query/ReactiveElasticsearchQueryExecution.java @@ -15,39 +15,46 @@ */ package org.springframework.data.elasticsearch.repository.query; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; - import org.springframework.core.convert.converter.Converter; +import org.springframework.data.elasticsearch.core.IndexCoordinates; import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations; import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.data.repository.query.ResultProcessor; import org.springframework.data.repository.query.ReturnedType; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; import org.springframework.util.ClassUtils; /** * @author Christoph Strobl + * @author Peter-Josef Meisch * @since 3.2 */ public interface ReactiveElasticsearchQueryExecution { - Object execute(Query query, Class type, String indexName, String indexType, @Nullable Class targetType); + Object execute(Query query, Class type, @Nullable Class targetType, IndexCoordinates indexCoordinates); /** * An {@link ReactiveElasticsearchQueryExecution} that wraps the results of the given delegate with the given result * processing. */ - @RequiredArgsConstructor final class ResultProcessingExecution implements ReactiveElasticsearchQueryExecution { - private final @NonNull ReactiveElasticsearchQueryExecution delegate; - private final @NonNull Converter converter; + private final ReactiveElasticsearchQueryExecution delegate; + private final Converter converter; + + public ResultProcessingExecution(ReactiveElasticsearchQueryExecution delegate, + Converter converter) { + Assert.notNull(delegate, "delegate must not be null"); + Assert.notNull(converter, "converter must not be null"); + this.delegate = delegate; + this.converter = converter; + } @Override - public Object execute(Query query, Class type, String indexName, String indexType, - @Nullable Class targetType) { - return converter.convert(delegate.execute(query, type, indexName, indexType, targetType)); + public Object execute(Query query, Class type, @Nullable Class targetType, + IndexCoordinates indexCoordinates) { + return converter.convert(delegate.execute(query, type, targetType, indexCoordinates)); } } @@ -56,11 +63,17 @@ public interface ReactiveElasticsearchQueryExecution { * * @author Mark Paluch */ - @RequiredArgsConstructor final class ResultProcessingConverter implements Converter { - private final @NonNull ResultProcessor processor; - private final @NonNull ReactiveElasticsearchOperations operations; + private final ResultProcessor processor; + private final ReactiveElasticsearchOperations operations; + + public ResultProcessingConverter(ResultProcessor processor, ReactiveElasticsearchOperations operations) { + Assert.notNull(processor, "processor must not be null"); + Assert.notNull(operations, "operations must not be null"); + this.processor = processor; + this.operations = operations; + } /* * (non-Javadoc) diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepository.java b/src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepository.java index fd503eb31..de2e74a47 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepository.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepository.java @@ -20,6 +20,7 @@ import reactor.core.publisher.Mono; import org.reactivestreams.Publisher; import org.springframework.data.domain.Sort; +import org.springframework.data.elasticsearch.core.IndexCoordinates; import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations; import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository; @@ -27,12 +28,14 @@ import org.springframework.util.Assert; /** * @author Christoph Strobl + * @author Peter-Josef Meisch * @since 3.2 */ public class SimpleReactiveElasticsearchRepository implements ReactiveElasticsearchRepository { private final ElasticsearchEntityInformation entityInformation; private final ReactiveElasticsearchOperations elasticsearchOperations; + private final IndexCoordinates index; public SimpleReactiveElasticsearchRepository(ElasticsearchEntityInformation entityInformation, ReactiveElasticsearchOperations elasticsearchOperations) { @@ -42,20 +45,20 @@ public class SimpleReactiveElasticsearchRepository implements ReactiveEla this.entityInformation = entityInformation; this.elasticsearchOperations = elasticsearchOperations; + this.index = IndexCoordinates.of(entityInformation.getIndexName()).withTypes(entityInformation.getType()); } @Override public Flux findAll(Sort sort) { - return elasticsearchOperations.find(Query.findAll().addSort(sort), entityInformation.getJavaType(), - entityInformation.getIndexName(), entityInformation.getType()); + return elasticsearchOperations.find(Query.findAll().addSort(sort), entityInformation.getJavaType(), index); } @Override public Mono save(S entity) { Assert.notNull(entity, "Entity must not be null!"); - return elasticsearchOperations.save(entity, entityInformation.getIndexName(), entityInformation.getType()); + return elasticsearchOperations.save(entity, index); } @Override @@ -76,8 +79,7 @@ public class SimpleReactiveElasticsearchRepository implements ReactiveEla public Mono findById(ID id) { Assert.notNull(id, "Id must not be null!"); - return elasticsearchOperations.findById(convertId(id), entityInformation.getJavaType(), - entityInformation.getIndexName(), entityInformation.getType()); + return elasticsearchOperations.findById(convertId(id), entityInformation.getJavaType(), index); } @Override @@ -91,8 +93,7 @@ public class SimpleReactiveElasticsearchRepository implements ReactiveEla public Mono existsById(ID id) { Assert.notNull(id, "Id must not be null!"); - return elasticsearchOperations.exists(convertId(id), entityInformation.getJavaType(), - entityInformation.getIndexName(), entityInformation.getType()); + return elasticsearchOperations.exists(convertId(id), entityInformation.getJavaType(), index); } @Override @@ -105,8 +106,7 @@ public class SimpleReactiveElasticsearchRepository implements ReactiveEla @Override public Flux findAll() { - return elasticsearchOperations.find(Query.findAll(), entityInformation.getJavaType(), - entityInformation.getIndexName(), entityInformation.getType()); + return elasticsearchOperations.find(Query.findAll(), entityInformation.getJavaType(), index); } @Override @@ -127,17 +127,14 @@ public class SimpleReactiveElasticsearchRepository implements ReactiveEla @Override public Mono count() { - return elasticsearchOperations.count(Query.findAll(), entityInformation.getJavaType(), - entityInformation.getIndexName(), entityInformation.getType()); + return elasticsearchOperations.count(Query.findAll(), entityInformation.getJavaType(), index); } @Override public Mono deleteById(ID id) { Assert.notNull(id, "Id must not be null!"); - return elasticsearchOperations - .deleteById(convertId(id), entityInformation.getJavaType(), entityInformation.getIndexName(), - entityInformation.getType()) // + return elasticsearchOperations.deleteById(convertId(id), entityInformation.getJavaType(), index) // .then(); } @@ -152,7 +149,7 @@ public class SimpleReactiveElasticsearchRepository implements ReactiveEla public Mono delete(T entity) { Assert.notNull(entity, "Entity must not be null!"); - return elasticsearchOperations.delete(entity, entityInformation.getIndexName(), entityInformation.getType()) // + return elasticsearchOperations.delete(entity, index) // .then(); } @@ -173,9 +170,7 @@ public class SimpleReactiveElasticsearchRepository implements ReactiveEla @Override public Mono deleteAll() { - return elasticsearchOperations - .deleteBy(Query.findAll(), entityInformation.getJavaType(), entityInformation.getIndexName(), - entityInformation.getType()) // + return elasticsearchOperations.deleteBy(Query.findAll(), entityInformation.getJavaType(), index) // .then(); } diff --git a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java index 5c6971cc3..67a8cf400 100755 --- a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java @@ -344,7 +344,7 @@ public abstract class ElasticsearchTemplateTests { // when NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_1_NAME, INDEX_2_NAME).withIndicesOptions(IndicesOptions.lenientExpandOpen()).build(); + .withIndicesOptions(IndicesOptions.lenientExpandOpen()).build(); Page entities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, IndexCoordinates.of(INDEX_1_NAME, INDEX_2_NAME)); @@ -512,9 +512,7 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_2_NAME)); // then - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("message", "foo")) - .withIndices(INDEX_1_NAME, INDEX_2_NAME) // - .build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("message", "foo")).build(); assertThat(elasticsearchTemplate.count(searchQuery, IndexCoordinates.of(INDEX_1_NAME, INDEX_2_NAME))).isEqualTo(0); } @@ -548,9 +546,7 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_2_NAME)); // then - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("message", "positive")) - .withIndices(INDEX_1_NAME, INDEX_2_NAME) // - .build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("message", "positive")).build(); assertThat(elasticsearchTemplate.count(searchQuery, IndexCoordinates.of("test-index-*"))).isEqualTo(2); } @@ -953,8 +949,8 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.index(indexQuery, index); elasticsearchTemplate.refresh(SampleEntity.class); - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).withFields("message").build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withFields("message") + .build(); // when Page page = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, index); @@ -986,7 +982,7 @@ public abstract class ElasticsearchTemplateTests { sourceFilter.withIncludes("message"); NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).withSourceFilter(sourceFilter.build()).build(); + .withSourceFilter(sourceFilter.build()).build(); // when Page page = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, index); @@ -1049,8 +1045,6 @@ public abstract class ElasticsearchTemplateTests { // then CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()); - criteriaQuery.addIndices(INDEX_NAME_SAMPLE_ENTITY); - criteriaQuery.addTypes(TYPE_NAME); criteriaQuery.setPageable(PageRequest.of(0, 10)); ScrolledPage scroll = elasticsearchTemplate.startScroll(1000, criteriaQuery, SampleEntity.class, @@ -1077,7 +1071,7 @@ public abstract class ElasticsearchTemplateTests { // then NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).withPageable(PageRequest.of(0, 10)).build(); + .withPageable(PageRequest.of(0, 10)).build(); ScrolledPage scroll = elasticsearchTemplate.startScroll(1000, searchQuery, SampleEntity.class, index); List sampleEntities = new ArrayList<>(); @@ -1101,8 +1095,6 @@ public abstract class ElasticsearchTemplateTests { // then CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()); - criteriaQuery.addIndices(INDEX_NAME_SAMPLE_ENTITY); - criteriaQuery.addTypes(TYPE_NAME); criteriaQuery.addFields("message"); criteriaQuery.setPageable(PageRequest.of(0, 10)); @@ -1130,9 +1122,8 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.refresh(SampleEntity.class); // then - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).withFields("message").withQuery(matchAllQuery()) - .withPageable(PageRequest.of(0, 10)).build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withFields("message") + .withQuery(matchAllQuery()).withPageable(PageRequest.of(0, 10)).build(); ScrolledPage scroll = elasticsearchTemplate.startScroll(1000, searchQuery, SampleEntity.class, index); String scrollId = scroll.getScrollId(); @@ -1158,8 +1149,6 @@ public abstract class ElasticsearchTemplateTests { // then CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()); - criteriaQuery.addIndices(INDEX_NAME_SAMPLE_ENTITY); - criteriaQuery.addTypes(TYPE_NAME); criteriaQuery.setPageable(PageRequest.of(0, 10)); ScrolledPage scroll = elasticsearchTemplate.startScroll(1000, criteriaQuery, SampleEntity.class, @@ -1187,7 +1176,7 @@ public abstract class ElasticsearchTemplateTests { // then NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).withPageable(PageRequest.of(0, 10)).build(); + .withPageable(PageRequest.of(0, 10)).build(); ScrolledPage scroll = elasticsearchTemplate.startScroll(1000, searchQuery, SampleEntity.class, index); String scrollId = scroll.getScrollId(); @@ -1266,8 +1255,6 @@ public abstract class ElasticsearchTemplateTests { // then CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()); - criteriaQuery.addIndices(INDEX_NAME_SAMPLE_ENTITY); - criteriaQuery.addTypes(TYPE_NAME); criteriaQuery.setPageable(PageRequest.of(0, 10)); CloseableIterator stream = elasticsearchTemplate.stream(criteriaQuery, SampleEntity.class, index); @@ -1456,8 +1443,8 @@ public abstract class ElasticsearchTemplateTests { .withUpdateRequest(updateRequest).build(); // when - UpdateRequest request = elasticsearchTemplate.getRequestFactory().updateRequest(updateQuery, - IndexCoordinates.of("index")); + UpdateRequest request = ((AbstractElasticsearchTemplate) elasticsearchTemplate).getRequestFactory() + .updateRequest(updateQuery, IndexCoordinates.of("index")); // then assertThat(request).isNotNull(); @@ -1478,8 +1465,8 @@ public abstract class ElasticsearchTemplateTests { .withUpdateRequest(updateRequest).build(); // when - UpdateRequest request = elasticsearchTemplate.getRequestFactory().updateRequest(updateQuery, - IndexCoordinates.of("index")); + UpdateRequest request = ((AbstractElasticsearchTemplate) elasticsearchTemplate).getRequestFactory() + .updateRequest(updateQuery, IndexCoordinates.of("index")); // then assertThat(request).isNotNull(); @@ -1524,7 +1511,7 @@ public abstract class ElasticsearchTemplateTests { // when NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_1_NAME, INDEX_2_NAME).withIndicesOptions(IndicesOptions.lenientExpandOpen()).build(); + .withIndicesOptions(IndicesOptions.lenientExpandOpen()).build(); List entities = new ArrayList<>(); @@ -1643,7 +1630,7 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.index(indexQuery, IndexCoordinates.of(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME)); elasticsearchTemplate.refresh(SampleEntity.class); NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("id", indexQuery.getId())) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).build(); + .build(); // then Page page = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, index); @@ -1672,7 +1659,7 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.bulkIndex(entities, index); elasticsearchTemplate.refresh(SampleEntity.class); NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("message", "message")) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).withPageable(PageRequest.of(0, 100)).build(); + .withPageable(PageRequest.of(0, 100)).build(); // then List ids = elasticsearchTemplate.queryForIds(searchQuery, SampleEntity.class, index); assertThat(ids).hasSize(30); @@ -1693,7 +1680,7 @@ public abstract class ElasticsearchTemplateTests { // when NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(boolQuery().must(wildcardQuery("message", "*a*")).should(wildcardQuery("message", "*b*"))) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).withMinScore(2.0F).build(); + .withMinScore(2.0F).build(); Page page = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, index); @@ -1823,8 +1810,7 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.refresh(index); // then - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withIndices(INDEX_NAME_SAMPLE_ENTITY) - .withTypes(TYPE_NAME).withQuery(matchAllQuery()).build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); Page sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, Map.class, index); assertThat(sampleEntities.getTotalElements()).isEqualTo(2); @@ -1847,8 +1833,7 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.index(indexQueryBuilder.build(), index); elasticsearchTemplate.refresh(index); - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withIndices(INDEX_NAME_SAMPLE_ENTITY) - .withTypes(TYPE_NAME).withQuery(matchAllQuery()).build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); // when Page entities = elasticsearchTemplate.queryForPage(searchQuery, GTEVersionEntity.class, index); // then @@ -1878,8 +1863,7 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.index(indexQuery, IndexCoordinates.of(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME)); elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_NAME_SAMPLE_ENTITY)); - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withIndices(INDEX_NAME_SAMPLE_ENTITY) - .withTypes(TYPE_NAME).withQuery(matchAllQuery()).build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); // when Page sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, index); @@ -1901,7 +1885,6 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.index(indexQuery, index); elasticsearchTemplate.refresh(SampleEntity.class); CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()); - criteriaQuery.addIndices(INDEX_NAME_SAMPLE_ENTITY); // when long count = elasticsearchTemplate.count(criteriaQuery, SampleEntity.class, index); @@ -1921,8 +1904,7 @@ public abstract class ElasticsearchTemplateTests { IndexQuery indexQuery = getIndexQuery(sampleEntity); elasticsearchTemplate.index(indexQuery, index); elasticsearchTemplate.refresh(SampleEntity.class); - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); // when long count = elasticsearchTemplate.count(searchQuery, SampleEntity.class, index); @@ -1943,12 +1925,10 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.index(indexQuery, index); elasticsearchTemplate.refresh(SampleEntity.class); CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()); - criteriaQuery.addIndices(INDEX_NAME_SAMPLE_ENTITY); - criteriaQuery.addTypes(TYPE_NAME); // when - long count = elasticsearchTemplate.count(criteriaQuery, index); + // then assertThat(count).isEqualTo(1); } @@ -1964,8 +1944,7 @@ public abstract class ElasticsearchTemplateTests { IndexQuery indexQuery = getIndexQuery(sampleEntity); elasticsearchTemplate.index(indexQuery, index); elasticsearchTemplate.refresh(SampleEntity.class); - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); // when long count = elasticsearchTemplate.count(searchQuery, index); @@ -1997,7 +1976,6 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_2_NAME)); CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()); - criteriaQuery.addIndices(INDEX_1_NAME, INDEX_2_NAME); // when long count = elasticsearchTemplate.count(criteriaQuery, IndexCoordinates.of(INDEX_1_NAME, INDEX_2_NAME)); @@ -2028,8 +2006,7 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_1_NAME)); elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_2_NAME)); - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_1_NAME, INDEX_2_NAME).build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); // when long count = elasticsearchTemplate.count(searchQuery, IndexCoordinates.of(INDEX_1_NAME, INDEX_2_NAME)); @@ -2096,7 +2073,6 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_2_NAME)); CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()); - criteriaQuery.addIndices(INDEX_1_NAME); // when long count = elasticsearchTemplate.count(criteriaQuery, IndexCoordinates.of(INDEX_1_NAME)); @@ -2127,8 +2103,7 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_1_NAME)); elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_2_NAME)); - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withIndices(INDEX_1_NAME) - .build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); // when long count = elasticsearchTemplate.count(searchQuery, IndexCoordinates.of(INDEX_1_NAME)); @@ -2264,8 +2239,7 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_1_NAME)); elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_2_NAME)); - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_1_NAME, INDEX_2_NAME).build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); // when List sampleEntities = elasticsearchTemplate.queryForList(searchQuery, SampleEntity.class, @@ -2294,8 +2268,7 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.refresh(IndexCoordinates.of(INDEX_2_NAME)); // when - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withTypes("hetro") - .withIndices(INDEX_1_NAME, INDEX_2_NAME).build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); Page page = elasticsearchTemplate.queryForPage(searchQuery, ResultAggregator.class, IndexCoordinates.of(INDEX_1_NAME, INDEX_2_NAME)); @@ -2444,8 +2417,6 @@ public abstract class ElasticsearchTemplateTests { // when CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("message").contains("message")); - criteriaQuery.addIndices(INDEX_NAME_SAMPLE_ENTITY); - criteriaQuery.addTypes(TYPE_NAME); criteriaQuery.setPageable(PageRequest.of(0, 10)); ScrolledPage scroll = elasticsearchTemplate.startScroll(1000, criteriaQuery, SampleEntity.class, @@ -2482,7 +2453,7 @@ public abstract class ElasticsearchTemplateTests { // when NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("message", "message")) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).withPageable(PageRequest.of(0, 10)).build(); + .withPageable(PageRequest.of(0, 10)).build(); ScrolledPage scroll = elasticsearchTemplate.startScroll(1000, searchQuery, SampleEntity.class, index); List sampleEntities = new ArrayList<>(); @@ -2512,8 +2483,7 @@ public abstract class ElasticsearchTemplateTests { SourceFilter sourceFilter = new FetchSourceFilter(new String[] { "id" }, new String[] {}); NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).withPageable(PageRequest.of(0, 10)) - .withSourceFilter(sourceFilter).build(); + .withPageable(PageRequest.of(0, 10)).withSourceFilter(sourceFilter).build(); ScrolledPage scroll = elasticsearchTemplate.startScroll(1000, searchQuery, SampleEntity.class, index); List sampleEntities = new ArrayList<>(); @@ -2638,8 +2608,8 @@ public abstract class ElasticsearchTemplateTests { elasticsearchTemplate.bulkIndex(indexQueries, index); elasticsearchTemplate.refresh(SampleEntity.class); - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) - .withIndices(INDEX_NAME_SAMPLE_ENTITY).withTypes(TYPE_NAME).withCollapseField("rate").build(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withCollapseField("rate") + .build(); // when Page page = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, index); @@ -2770,8 +2740,6 @@ public abstract class ElasticsearchTemplateTests { NativeSearchQuery query = new NativeSearchQueryBuilder() // .withQuery(matchAllQuery()) // - .withIndices(alias) // - .withTypes(TYPE_NAME) // .build(); long count = elasticsearchTemplate.count(query, IndexCoordinates.of(alias)); diff --git a/src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplateTests.java b/src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplateTests.java index e33821692..3b3193ffd 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplateTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplateTests.java @@ -163,14 +163,15 @@ public class ReactiveElasticsearchTemplateTests { public void insertWithExplicitIndexNameShouldOverwriteMetadata() { SampleEntity sampleEntity = randomEntity("in another index"); + IndexCoordinates alternateIndex = IndexCoordinates.of(ALTERNATE_INDEX); - template.save(sampleEntity, ALTERNATE_INDEX) // + template.save(sampleEntity, alternateIndex) // .as(StepVerifier::create)// .expectNextCount(1)// .verifyComplete(); restTemplate.refresh(IndexCoordinates.of(DEFAULT_INDEX)); - restTemplate.refresh(IndexCoordinates.of(ALTERNATE_INDEX)); + restTemplate.refresh(alternateIndex); assertThat(TestUtils.documentWithId(sampleEntity.getId()).existsIn(DEFAULT_INDEX)).isFalse(); assertThat(TestUtils.documentWithId(sampleEntity.getId()).existsIn(ALTERNATE_INDEX)).isTrue(); @@ -181,7 +182,7 @@ public class ReactiveElasticsearchTemplateTests { Map map = new LinkedHashMap<>(Collections.singletonMap("foo", "bar")); - template.save(map, ALTERNATE_INDEX, "singleton-map") // + template.save(map, IndexCoordinates.of(ALTERNATE_INDEX).withTypes("singleton-map")) // .as(StepVerifier::create) // .consumeNextWith(actual -> { assertThat(map).containsKey("id"); @@ -198,7 +199,7 @@ public class ReactiveElasticsearchTemplateTests { @Test // DATAES-519 public void findByIdShouldCompleteWhenIndexDoesNotExist() { - template.findById("foo", SampleEntity.class, "no-such-index") // + template.findById("foo", SampleEntity.class, IndexCoordinates.of("no-such-index").withTypes("test-type")) // .as(StepVerifier::create) // .verifyComplete(); } @@ -256,17 +257,20 @@ public class ReactiveElasticsearchTemplateTests { IndexQuery indexQuery = getIndexQuery(sampleEntity); - restTemplate.index(indexQuery, IndexCoordinates.of(ALTERNATE_INDEX).withTypes( "test-type")); + IndexCoordinates defaultIndex = IndexCoordinates.of(DEFAULT_INDEX).withTypes("test-type"); + IndexCoordinates alternateIndex = IndexCoordinates.of(ALTERNATE_INDEX).withTypes("test-type"); + + restTemplate.index(indexQuery, alternateIndex); restTemplate.refresh(SampleEntity.class); - restTemplate.refresh(IndexCoordinates.of(DEFAULT_INDEX)); - restTemplate.refresh(IndexCoordinates.of(ALTERNATE_INDEX)); + restTemplate.refresh(defaultIndex); + restTemplate.refresh(alternateIndex); - template.findById(sampleEntity.getId(), SampleEntity.class) // + template.findById(sampleEntity.getId(), SampleEntity.class, defaultIndex) // .as(StepVerifier::create) // .verifyComplete(); - template.findById(sampleEntity.getId(), SampleEntity.class, ALTERNATE_INDEX) // + template.findById(sampleEntity.getId(), SampleEntity.class, alternateIndex) // .as(StepVerifier::create)// .expectNextCount(1) // .verifyComplete(); @@ -275,7 +279,7 @@ public class ReactiveElasticsearchTemplateTests { @Test // DATAES-519 public void existsShouldReturnFalseWhenIndexDoesNotExist() { - template.exists("foo", SampleEntity.class, "no-such-index") // + template.exists("foo", SampleEntity.class, IndexCoordinates.of("no-such-index")) // .as(StepVerifier::create) // .expectNext(false) // .verifyComplete(); @@ -308,7 +312,9 @@ public class ReactiveElasticsearchTemplateTests { @Test // DATAES-519 public void findShouldCompleteWhenIndexDoesNotExist() { - template.find(new CriteriaQuery(Criteria.where("message").is("some message")), SampleEntity.class, "no-such-index") // + template + .find(new CriteriaQuery(Criteria.where("message").is("some message")), SampleEntity.class, + IndexCoordinates.of("no-such-index")) // .as(StepVerifier::create) // .verifyComplete(); } @@ -509,7 +515,7 @@ public class ReactiveElasticsearchTemplateTests { @Test // DATAES-519 public void deleteByIdShouldCompleteWhenIndexDoesNotExist() { - template.deleteById("does-not-exists", SampleEntity.class, "no-such-index") // + template.deleteById("does-not-exists", SampleEntity.class, IndexCoordinates.of("no-such-index")) // .as(StepVerifier::create)// .verifyComplete(); } @@ -532,7 +538,7 @@ public class ReactiveElasticsearchTemplateTests { SampleEntity sampleEntity = randomEntity("test message"); index(sampleEntity); - template.deleteById(sampleEntity.getId(), DEFAULT_INDEX, "test-type") // + template.deleteById(sampleEntity.getId(), IndexCoordinates.of(DEFAULT_INDEX).withTypes("test-type")) // .as(StepVerifier::create)// .expectNext(sampleEntity.getId()) // .verifyComplete(); @@ -577,8 +583,8 @@ public class ReactiveElasticsearchTemplateTests { public void shouldDeleteAcrossIndex() { String indexPrefix = "rx-template-test-index"; - String thisIndex = indexPrefix + "-this"; - String thatIndex = indexPrefix + "-that"; + IndexCoordinates thisIndex = IndexCoordinates.of(indexPrefix + "-this"); + IndexCoordinates thatIndex = IndexCoordinates.of(indexPrefix + "-that"); template.save(randomEntity("test"), thisIndex) // .then(template.save(randomEntity("test"), thatIndex)) // @@ -586,20 +592,19 @@ public class ReactiveElasticsearchTemplateTests { .as(StepVerifier::create)// .verifyComplete(); - restTemplate.refresh(IndexCoordinates.of(thisIndex)); - restTemplate.refresh(IndexCoordinates.of(thatIndex)); + restTemplate.refresh(thisIndex); + restTemplate.refresh(thatIndex); NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() // .withQuery(termQuery("message", "test")) // - .withIndices(indexPrefix + "*") // .build(); - template.deleteBy(searchQuery, SampleEntity.class) // + template.deleteBy(searchQuery, SampleEntity.class, IndexCoordinates.of(indexPrefix + '*')) // .as(StepVerifier::create) // .expectNext(2L) // .verifyComplete(); - TestUtils.deleteIndex(thisIndex, thatIndex); + TestUtils.deleteIndex(thisIndex.getIndexName(), thatIndex.getIndexName()); } @Test // DATAES-547 @@ -607,8 +612,8 @@ public class ReactiveElasticsearchTemplateTests { public void shouldDeleteAcrossIndexWhenNoMatchingDataPresent() { String indexPrefix = "rx-template-test-index"; - String thisIndex = indexPrefix + "-this"; - String thatIndex = indexPrefix + "-that"; + IndexCoordinates thisIndex = IndexCoordinates.of(indexPrefix + "-this"); + IndexCoordinates thatIndex = IndexCoordinates.of(indexPrefix + "-that"); template.save(randomEntity("positive"), thisIndex) // .then(template.save(randomEntity("positive"), thatIndex)) // @@ -616,20 +621,19 @@ public class ReactiveElasticsearchTemplateTests { .as(StepVerifier::create)// .verifyComplete(); - restTemplate.refresh(IndexCoordinates.of(thisIndex)); - restTemplate.refresh(IndexCoordinates.of(thatIndex)); + restTemplate.refresh(thisIndex); + restTemplate.refresh(thatIndex); NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() // .withQuery(termQuery("message", "negative")) // - .withIndices(indexPrefix + "*") // .build(); - template.deleteBy(searchQuery, SampleEntity.class) // + template.deleteBy(searchQuery, SampleEntity.class, IndexCoordinates.of(indexPrefix + '*')) // .as(StepVerifier::create) // .expectNext(0L) // .verifyComplete(); - TestUtils.deleteIndex(thisIndex, thatIndex); + TestUtils.deleteIndex(thisIndex.getIndexName(), thatIndex.getIndexName()); } @Test // DATAES-504 @@ -672,13 +676,12 @@ public class ReactiveElasticsearchTemplateTests { index(entity1, entity2, entity3); NativeSearchQuery query = new NativeSearchQueryBuilder() // - .withIndices(DEFAULT_INDEX) // .withQuery(matchAllQuery()) // .withCollapseField("rate") // .withPageable(PageRequest.of(0, 25)) // .build(); - template.find(query, SampleEntity.class) // + template.find(query, SampleEntity.class, IndexCoordinates.of(DEFAULT_INDEX)) // .as(StepVerifier::create) // .expectNextCount(2) // .verifyComplete(); @@ -725,7 +728,7 @@ public class ReactiveElasticsearchTemplateTests { private void index(SampleEntity... entities) { - IndexCoordinates indexCoordinates = IndexCoordinates.of(DEFAULT_INDEX).withTypes( "test-type"); + IndexCoordinates indexCoordinates = IndexCoordinates.of(DEFAULT_INDEX).withTypes("test-type"); if (entities.length == 1) { restTemplate.index(getIndexQuery(entities[0]), indexCoordinates); diff --git a/src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplateUnitTests.java b/src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplateUnitTests.java index 066b918e6..d4c4b7b43 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplateUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplateUnitTests.java @@ -57,6 +57,7 @@ import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsea import org.springframework.data.elasticsearch.core.geo.GeoPoint; import org.springframework.data.elasticsearch.core.query.Criteria; import org.springframework.data.elasticsearch.core.query.CriteriaQuery; +import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.data.elasticsearch.core.query.StringQuery; /** @@ -69,6 +70,8 @@ public class ReactiveElasticsearchTemplateUnitTests { @Mock ReactiveElasticsearchClient client; ReactiveElasticsearchTemplate template; + private IndexCoordinates index = IndexCoordinates.of("index").withTypes("type"); + @BeforeEach public void setUp() { @@ -81,7 +84,7 @@ public class ReactiveElasticsearchTemplateUnitTests { ArgumentCaptor captor = ArgumentCaptor.forClass(IndexRequest.class); when(client.index(captor.capture())).thenReturn(Mono.empty()); - template.save(Collections.singletonMap("key", "value"), "index", "type") // + template.save(Collections.singletonMap("key", "value"), index) // .as(StepVerifier::create) // .verifyComplete(); @@ -96,7 +99,7 @@ public class ReactiveElasticsearchTemplateUnitTests { template.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL); - template.save(Collections.singletonMap("key", "value"), "index", "type") // + template.save(Collections.singletonMap("key", "value"), index) // .as(StepVerifier::create) // .verifyComplete(); @@ -124,7 +127,8 @@ public class ReactiveElasticsearchTemplateUnitTests { template.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN); - template.find(new CriteriaQuery(new Criteria("*")).setPageable(PageRequest.of(0, 10)), SampleEntity.class) // + Query query = new CriteriaQuery(new Criteria("*")).setPageable(PageRequest.of(0, 10)); + template.find(query, SampleEntity.class, index) // .as(StepVerifier::create) // .verifyComplete(); @@ -137,7 +141,8 @@ public class ReactiveElasticsearchTemplateUnitTests { ArgumentCaptor captor = ArgumentCaptor.forClass(SearchRequest.class); when(client.search(captor.capture())).thenReturn(Flux.empty()); - template.find(new CriteriaQuery(new Criteria("*")).setPageable(PageRequest.of(2, 50)), SampleEntity.class) // + Query query = new CriteriaQuery(new Criteria("*")).setPageable(PageRequest.of(2, 50)); + template.find(query, SampleEntity.class, index) // .as(StepVerifier::create) // .verifyComplete(); @@ -164,7 +169,7 @@ public class ReactiveElasticsearchTemplateUnitTests { ArgumentCaptor captor = ArgumentCaptor.forClass(DeleteRequest.class); when(client.delete(captor.capture())).thenReturn(Mono.empty()); - template.deleteById("id", "index", "type") // + template.deleteById("id", index) // .as(StepVerifier::create) // .verifyComplete(); @@ -179,7 +184,7 @@ public class ReactiveElasticsearchTemplateUnitTests { template.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL); - template.deleteById("id", "index", "type") // + template.deleteById("id", index) // .as(StepVerifier::create) // .verifyComplete(); @@ -192,7 +197,7 @@ public class ReactiveElasticsearchTemplateUnitTests { ArgumentCaptor captor = ArgumentCaptor.forClass(DeleteByQueryRequest.class); when(client.deleteBy(captor.capture())).thenReturn(Mono.empty()); - template.deleteBy(new StringQuery(QueryBuilders.matchAllQuery().toString()), Object.class, "index", "type") // + template.deleteBy(new StringQuery(QueryBuilders.matchAllQuery().toString()), Object.class, index) // .as(StepVerifier::create) // .verifyComplete(); @@ -207,7 +212,7 @@ public class ReactiveElasticsearchTemplateUnitTests { template.setRefreshPolicy(RefreshPolicy.NONE); - template.deleteBy(new StringQuery(QueryBuilders.matchAllQuery().toString()), Object.class, "index", "type") // + template.deleteBy(new StringQuery(QueryBuilders.matchAllQuery().toString()), Object.class, index) // .as(StepVerifier::create) // .verifyComplete(); @@ -220,7 +225,7 @@ public class ReactiveElasticsearchTemplateUnitTests { ArgumentCaptor captor = ArgumentCaptor.forClass(DeleteByQueryRequest.class); when(client.deleteBy(captor.capture())).thenReturn(Mono.empty()); - template.deleteBy(new StringQuery(QueryBuilders.matchAllQuery().toString()), Object.class, "index", "type") // + template.deleteBy(new StringQuery(QueryBuilders.matchAllQuery().toString()), Object.class, index) // .as(StepVerifier::create) // .verifyComplete(); @@ -235,7 +240,7 @@ public class ReactiveElasticsearchTemplateUnitTests { template.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN); - template.deleteBy(new StringQuery(QueryBuilders.matchAllQuery().toString()), Object.class, "index", "type") // + template.deleteBy(new StringQuery(QueryBuilders.matchAllQuery().toString()), Object.class, index) // .as(StepVerifier::create) // .verifyComplete(); diff --git a/src/test/java/org/springframework/data/elasticsearch/core/aggregation/ElasticsearchTemplateAggregationTests.java b/src/test/java/org/springframework/data/elasticsearch/core/aggregation/ElasticsearchTemplateAggregationTests.java index af2005791..4f0a3dbdf 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/aggregation/ElasticsearchTemplateAggregationTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/aggregation/ElasticsearchTemplateAggregationTests.java @@ -90,7 +90,7 @@ public class ElasticsearchTemplateAggregationTests { .addAuthor(RIZWAN_IDREES).addPublishedYear(YEAR_2002).addPublishedYear(YEAR_2001).addPublishedYear(YEAR_2000) .score(40).buildIndex(); - IndexCoordinates index = IndexCoordinates.of(INDEX_NAME).withTypes( "article"); + IndexCoordinates index = IndexCoordinates.of(INDEX_NAME).withTypes("article"); elasticsearchTemplate.index(article1, index); elasticsearchTemplate.index(article2, index); elasticsearchTemplate.index(article3, index); @@ -111,7 +111,6 @@ public class ElasticsearchTemplateAggregationTests { NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() // .withQuery(matchAllQuery()) // .withSearchType(SearchType.DEFAULT) // - .withIndices(INDEX_NAME).withTypes("article") // .addAggregation(terms("subjects").field("subject")) // .build(); // when diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/spel/SpELEntityTests.java b/src/test/java/org/springframework/data/elasticsearch/repositories/spel/SpELEntityTests.java index 2e8a0e53b..fe7acedc8 100644 --- a/src/test/java/org/springframework/data/elasticsearch/repositories/spel/SpELEntityTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/spel/SpELEntityTests.java @@ -70,7 +70,6 @@ public class SpELEntityTests { // then NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(QueryBuilders.matchAllQuery()); - nativeSearchQuery.addIndices("test-index-abz-entity"); long count = template.count(nativeSearchQuery, IndexCoordinates.of("test-index-abz-entity")); assertThat(count).isEqualTo(2); } @@ -86,8 +85,6 @@ public class SpELEntityTests { // then NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(QueryBuilders.matchAllQuery()); - nativeSearchQuery.addIndices("test-index-abz-entity"); - nativeSearchQuery.addTypes("myType"); long count = template.count(nativeSearchQuery, IndexCoordinates.of("test-index-abz-entity")); assertThat(count).isEqualTo(1); }