Skip to content

Commit e8e110e

Browse files
committed
Add VectorIndex and SearchIndexDefinition abstraction.
See #4706 Original pull request: #4882
1 parent 1645d6c commit e8e110e

22 files changed

+1429
-953
lines changed

spring-data-mongodb/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@
131131
<optional>true</optional>
132132
</dependency>
133133

134+
<dependency>
135+
<groupId>org.awaitility</groupId>
136+
<artifactId>awaitility</artifactId>
137+
<version>4.2.2</version>
138+
<scope>test</scope>
139+
</dependency>
140+
134141
<dependency>
135142
<groupId>io.reactivex.rxjava3</groupId>
136143
<artifactId>rxjava</artifactId>

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

+22-22
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@
185185
* @author Michael Krog
186186
* @author Jakub Zurawa
187187
*/
188-
public class MongoTemplate
189-
implements MongoOperations, ApplicationContextAware, IndexOperationsProvider, SearchIndexOperationsProvider, ReadPreferenceAware {
188+
public class MongoTemplate implements MongoOperations, ApplicationContextAware, IndexOperationsProvider,
189+
SearchIndexOperationsProvider, ReadPreferenceAware {
190190

191191
private static final Log LOGGER = LogFactory.getLog(MongoTemplate.class);
192192
private static final WriteResultChecking DEFAULT_WRITE_RESULT_CHECKING = WriteResultChecking.NONE;
@@ -771,6 +771,21 @@ public IndexOperations indexOps(Class<?> entityClass) {
771771
return indexOps(getCollectionName(entityClass), entityClass);
772772
}
773773

774+
@Override
775+
public SearchIndexOperations searchIndexOps(String collectionName) {
776+
return searchIndexOps(null, collectionName);
777+
}
778+
779+
@Override
780+
public SearchIndexOperations searchIndexOps(Class<?> type) {
781+
return new DefaultSearchIndexOperations(this, type);
782+
}
783+
784+
@Override
785+
public SearchIndexOperations searchIndexOps(@Nullable Class<?> type, String collectionName) {
786+
return new DefaultSearchIndexOperations(this, collectionName, type);
787+
}
788+
774789
@Override
775790
public BulkOperations bulkOps(BulkMode mode, String collectionName) {
776791
return bulkOps(mode, null, collectionName);
@@ -1316,7 +1331,7 @@ private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc
13161331

13171332
if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)) {
13181333
if (wc == null || wc.getWObject() == null
1319-
|| (wc.getWObject()instanceof Number concern && concern.intValue() < 1)) {
1334+
|| (wc.getWObject() instanceof Number concern && concern.intValue() < 1)) {
13201335
return WriteConcern.ACKNOWLEDGED;
13211336
}
13221337
}
@@ -1968,7 +1983,8 @@ public <T> List<T> mapReduce(Query query, Class<?> domainType, String inputColle
19681983
}
19691984

19701985
if (mapReduceOptions.getOutputSharded().isPresent()) {
1971-
MongoCompatibilityAdapter.mapReduceIterableAdapter(mapReduce).sharded(mapReduceOptions.getOutputSharded().get());
1986+
MongoCompatibilityAdapter.mapReduceIterableAdapter(mapReduce)
1987+
.sharded(mapReduceOptions.getOutputSharded().get());
19721988
}
19731989

19741990
if (StringUtils.hasText(mapReduceOptions.getOutputCollection()) && !mapReduceOptions.usesInlineOutput()) {
@@ -2067,7 +2083,7 @@ public <T> List<T> findAllAndRemove(Query query, Class<T> entityClass, String co
20672083
}
20682084

20692085
@Override
2070-
public <T> UpdateResult replace(Query query, T replacement, ReplaceOptions options, String collectionName){
2086+
public <T> UpdateResult replace(Query query, T replacement, ReplaceOptions options, String collectionName) {
20712087

20722088
Assert.notNull(replacement, "Replacement must not be null");
20732089
return replace(query, (Class<T>) ClassUtils.getUserClass(replacement), replacement, options, collectionName);
@@ -2743,8 +2759,7 @@ protected <T> T doFindAndModify(CollectionPreparer collectionPreparer, String co
27432759
LOGGER.debug(String.format(
27442760
"findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s in collection: %s",
27452761
serializeToJsonSafely(mappedQuery), fields, serializeToJsonSafely(sort), entityClass,
2746-
serializeToJsonSafely(mappedUpdate),
2747-
collectionName));
2762+
serializeToJsonSafely(mappedUpdate), collectionName));
27482763
}
27492764

27502765
return executeFindOneInternal(
@@ -3013,21 +3028,6 @@ static RuntimeException potentiallyConvertRuntimeException(RuntimeException ex,
30133028
return resolved == null ? ex : resolved;
30143029
}
30153030

3016-
@Override
3017-
public SearchIndexOperations searchIndexOps(String collectionName) {
3018-
return searchIndexOps(null, collectionName);
3019-
}
3020-
3021-
@Override
3022-
public SearchIndexOperations searchIndexOps(Class<?> type) {
3023-
return new DefaultSearchIndexOperations(this, type);
3024-
}
3025-
3026-
@Override
3027-
public SearchIndexOperations searchIndexOps(Class<?> type, String collectionName) {
3028-
return new DefaultSearchIndexOperations(this, collectionName, type);
3029-
}
3030-
30313031
// Callback implementations
30323032

30333033
/**

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java

+23-9
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,9 @@ public static UnwindOperation unwind(String field, String arrayIndex) {
381381
}
382382

383383
/**
384-
* Factory method to create a new {@link UnwindOperation} for the field with the given name, including the name of a new
385-
* field to hold the array index of the element as {@code arrayIndex} using {@code preserveNullAndEmptyArrays}. Note
386-
* that extended unwind is supported in MongoDB version 3.2+.
384+
* Factory method to create a new {@link UnwindOperation} for the field with the given name, including the name of a
385+
* new field to hold the array index of the element as {@code arrayIndex} using {@code preserveNullAndEmptyArrays}.
386+
* Note that extended unwind is supported in MongoDB version 3.2+.
387387
*
388388
* @param field must not be {@literal null} or empty.
389389
* @param arrayIndex must not be {@literal null} or empty.
@@ -428,6 +428,20 @@ public static StartWithBuilder graphLookup(String fromCollection) {
428428
return GraphLookupOperation.builder().from(fromCollection);
429429
}
430430

431+
/**
432+
* Creates a new {@link VectorSearchOperation} by starting from the {@code indexName} to use.
433+
*
434+
* @param indexName must not be {@literal null} or empty.
435+
* @return new instance of {@link VectorSearchOperation.PathContributor}.
436+
* @since 4.5
437+
*/
438+
public static VectorSearchOperation.PathContributor vectorSearch(String indexName) {
439+
440+
Assert.hasText(indexName, "Index name must not be null or empty");
441+
442+
return VectorSearchOperation.search(indexName);
443+
}
444+
431445
/**
432446
* Factory method to create a new {@link SortOperation} for the given {@link Sort}.
433447
*
@@ -669,14 +683,14 @@ public static LookupOperation lookup(Field from, Field localField, Field foreign
669683

670684
/**
671685
* Entrypoint for creating {@link LookupOperation $lookup} using a fluent builder API.
686+
*
672687
* <pre class="code">
673-
* Aggregation.lookup().from("restaurants")
674-
* .localField("restaurant_name")
675-
* .foreignField("name")
676-
* .let(newVariable("orders_drink").forField("drink"))
677-
* .pipeline(match(ctx -> new Document("$expr", new Document("$in", List.of("$$orders_drink", "$beverages")))))
678-
* .as("matches")
688+
* Aggregation.lookup().from("restaurants").localField("restaurant_name").foreignField("name")
689+
* .let(newVariable("orders_drink").forField("drink"))
690+
* .pipeline(match(ctx -> new Document("$expr", new Document("$in", List.of("$$orders_drink", "$beverages")))))
691+
* .as("matches")
679692
* </pre>
693+
*
680694
* @return new instance of {@link LookupOperationBuilder}.
681695
* @since 4.1
682696
*/

0 commit comments

Comments
 (0)