|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.example.spi.mongodb.atlas; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import org.bson.Document; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.core.ResolvableType; |
| 23 | +import org.springframework.data.domain.Limit; |
| 24 | +import org.springframework.data.mongodb.core.MongoOperations; |
| 25 | +import org.springframework.data.mongodb.core.aggregation.Aggregation; |
| 26 | +import org.springframework.data.repository.core.RepositoryMetadata; |
| 27 | +import org.springframework.data.repository.core.RepositoryMethodContext; |
| 28 | +import org.springframework.data.repository.core.support.RepositoryMetadataAccess; |
| 29 | + |
| 30 | +class AtlasRepositoryFragment<T> implements AtlasRepository<T>, RepositoryMetadataAccess { |
| 31 | + |
| 32 | + private final MongoOperations mongoOperations; |
| 33 | + |
| 34 | + public AtlasRepositoryFragment(@Autowired MongoOperations mongoOperations) { |
| 35 | + this.mongoOperations = mongoOperations; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + @SuppressWarnings("unchecked") |
| 40 | + public List<T> vectorSearch(String index, String path, List<Double> vector) { |
| 41 | + |
| 42 | + RepositoryMethodContext methodContext = RepositoryMethodContext.getContext(); |
| 43 | + |
| 44 | + Class<?> domainType = resolveDomainType(methodContext.getMetadata()); |
| 45 | + |
| 46 | + Document $vectorSearch = createDocument(index, path, vector, Limit.of(10)); |
| 47 | + Aggregation aggregation = Aggregation.newAggregation(ctx -> $vectorSearch); |
| 48 | + |
| 49 | + return (List<T>) mongoOperations.aggregate(aggregation, mongoOperations.getCollectionName(domainType), domainType).getMappedResults(); |
| 50 | + } |
| 51 | + |
| 52 | + @SuppressWarnings("unchecked") |
| 53 | + private static <T> Class<T> resolveDomainType(RepositoryMetadata metadata) { |
| 54 | + |
| 55 | + // resolve the actual generic type argument of the AtlasRepository<T>. |
| 56 | + return (Class<T>) ResolvableType.forClass(metadata.getRepositoryInterface()) |
| 57 | + .as(AtlasRepository.class) |
| 58 | + .getGeneric(0) |
| 59 | + .resolve(); |
| 60 | + } |
| 61 | + |
| 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 | +} |
0 commit comments