1
1
/*
2
- * Copyright 2024 the original author or authors.
2
+ * Copyright 2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
7
*
8
- * https ://www.apache.org/licenses/LICENSE-2.0
8
+ * http ://www.apache.org/licenses/LICENSE-2.0
9
9
*
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
17
17
18
18
import java .util .List ;
19
19
20
- import org .bson .Document ;
21
20
import org .springframework .beans .factory .annotation .Autowired ;
22
21
import org .springframework .core .ResolvableType ;
23
22
import org .springframework .data .domain .Limit ;
24
23
import org .springframework .data .mongodb .core .MongoOperations ;
25
24
import org .springframework .data .mongodb .core .aggregation .Aggregation ;
25
+ import org .springframework .data .mongodb .core .aggregation .VectorSearchOperation ;
26
26
import org .springframework .data .repository .core .RepositoryMetadata ;
27
27
import org .springframework .data .repository .core .RepositoryMethodContext ;
28
28
import org .springframework .data .repository .core .support .RepositoryMetadataAccess ;
@@ -43,8 +43,10 @@ public List<T> vectorSearch(String index, String path, List<Double> vector) {
43
43
44
44
Class <?> domainType = resolveDomainType (methodContext .getMetadata ());
45
45
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 );
48
50
49
51
return (List <T >) mongoOperations .aggregate (aggregation , mongoOperations .getCollectionName (domainType ), domainType ).getMappedResults ();
50
52
}
@@ -59,16 +61,4 @@ private static <T> Class<T> resolveDomainType(RepositoryMetadata metadata) {
59
61
.resolve ();
60
62
}
61
63
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
- }
74
64
}
0 commit comments