Skip to content

Commit e145037

Browse files
committed
Use VectorSearchOperation instead of Document.
See #693
1 parent 1607cc0 commit e145037

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

mongodb/fragment-spi/atlas-api/src/main/java/com/example/spi/mongodb/atlas/AtlasRepositoryFragment.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2024 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,12 +17,12 @@
1717

1818
import java.util.List;
1919

20-
import org.bson.Document;
2120
import org.springframework.beans.factory.annotation.Autowired;
2221
import org.springframework.core.ResolvableType;
2322
import org.springframework.data.domain.Limit;
2423
import org.springframework.data.mongodb.core.MongoOperations;
2524
import org.springframework.data.mongodb.core.aggregation.Aggregation;
25+
import org.springframework.data.mongodb.core.aggregation.VectorSearchOperation;
2626
import org.springframework.data.repository.core.RepositoryMetadata;
2727
import org.springframework.data.repository.core.RepositoryMethodContext;
2828
import org.springframework.data.repository.core.support.RepositoryMetadataAccess;
@@ -43,8 +43,10 @@ public List<T> vectorSearch(String index, String path, List<Double> vector) {
4343

4444
Class<?> domainType = resolveDomainType(methodContext.getMetadata());
4545

46-
Document $vectorSearch = createDocument(index, path, vector, Limit.of(10));
47-
Aggregation aggregation = Aggregation.newAggregation(ctx -> $vectorSearch);
46+
VectorSearchOperation $vectorSearch = VectorSearchOperation.search(index).path(path).vector(vector)
47+
.limit(Limit.of(10)).numCandidates(150);
48+
49+
Aggregation aggregation = Aggregation.newAggregation($vectorSearch);
4850

4951
return (List<T>) mongoOperations.aggregate(aggregation, mongoOperations.getCollectionName(domainType), domainType).getMappedResults();
5052
}
@@ -59,16 +61,4 @@ private static <T> Class<T> resolveDomainType(RepositoryMetadata metadata) {
5961
.resolve();
6062
}
6163

62-
private static Document createDocument(String indexName, String path, List<Double> vector, Limit limit) {
63-
64-
Document $vectorSearch = new Document();
65-
66-
$vectorSearch.append("index", indexName);
67-
$vectorSearch.append("path", path);
68-
$vectorSearch.append("queryVector", vector);
69-
$vectorSearch.append("limit", limit.max());
70-
$vectorSearch.append("numCandidates", 150);
71-
72-
return new Document("$vectorSearch", $vectorSearch);
73-
}
7464
}

0 commit comments

Comments
 (0)