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,
1717
1818import java .util .List ;
1919
20- import org .bson .Document ;
2120import org .springframework .beans .factory .annotation .Autowired ;
2221import org .springframework .core .ResolvableType ;
2322import org .springframework .data .domain .Limit ;
2423import org .springframework .data .mongodb .core .MongoOperations ;
2524import org .springframework .data .mongodb .core .aggregation .Aggregation ;
25+ import org .springframework .data .mongodb .core .aggregation .VectorSearchOperation ;
2626import org .springframework .data .repository .core .RepositoryMetadata ;
2727import org .springframework .data .repository .core .RepositoryMethodContext ;
2828import 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