|
| 1 | +/* |
| 2 | + * Copyright 2025 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 org.springframework.data.mongodb.repository.query; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import java.lang.reflect.Method; |
| 22 | + |
| 23 | +import org.jetbrains.annotations.NotNull; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.data.domain.Limit; |
| 27 | +import org.springframework.data.domain.Score; |
| 28 | +import org.springframework.data.domain.SearchResults; |
| 29 | +import org.springframework.data.domain.Vector; |
| 30 | +import org.springframework.data.mapping.model.ValueExpressionEvaluator; |
| 31 | +import org.springframework.data.mongodb.core.aggregation.VectorSearchOperation; |
| 32 | +import org.springframework.data.mongodb.core.convert.MappingMongoConverter; |
| 33 | +import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver; |
| 34 | +import org.springframework.data.mongodb.core.mapping.MongoMappingContext; |
| 35 | +import org.springframework.data.mongodb.repository.VectorSearch; |
| 36 | +import org.springframework.data.mongodb.util.json.ParameterBindingContext; |
| 37 | +import org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec; |
| 38 | +import org.springframework.data.projection.SpelAwareProxyProjectionFactory; |
| 39 | +import org.springframework.data.repository.Repository; |
| 40 | +import org.springframework.data.repository.core.RepositoryMetadata; |
| 41 | +import org.springframework.data.repository.core.support.AnnotationRepositoryMetadata; |
| 42 | +import org.springframework.data.repository.query.ValueExpressionDelegate; |
| 43 | + |
| 44 | +/** |
| 45 | + * Unit tests for {@link VectorSearchDelegate}. |
| 46 | + * |
| 47 | + * @author Mark Paluch |
| 48 | + */ |
| 49 | +class VectorSearchDelegateUnitTests { |
| 50 | + |
| 51 | + MappingMongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, new MongoMappingContext()); |
| 52 | + |
| 53 | + @Test |
| 54 | + void shouldConsiderDerivedLimit() throws ReflectiveOperationException { |
| 55 | + |
| 56 | + Method method = VectorSearchRepository.class.getMethod("searchTop10ByEmbeddingNear", Vector.class, Score.class); |
| 57 | + |
| 58 | + MongoQueryMethod queryMethod = getMongoQueryMethod(method); |
| 59 | + MongoParametersParameterAccessor accessor = getAccessor(queryMethod, Vector.of(1, 2), Score.of(1)); |
| 60 | + |
| 61 | + VectorSearchDelegate.QueryMetadata query = createQueryMetadata(queryMethod, accessor); |
| 62 | + |
| 63 | + assertThat(query.query().getLimit()).isEqualTo(10); |
| 64 | + assertThat(query.numCandidates()).isEqualTo(10 * 20); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void shouldNotSetNumCandidates() throws ReflectiveOperationException { |
| 69 | + |
| 70 | + Method method = VectorSearchRepository.class.getMethod("searchTop10EnnByEmbeddingNear", Vector.class, Score.class); |
| 71 | + |
| 72 | + MongoQueryMethod queryMethod = getMongoQueryMethod(method); |
| 73 | + MongoParametersParameterAccessor accessor = getAccessor(queryMethod, Vector.of(1, 2), Score.of(1)); |
| 74 | + |
| 75 | + VectorSearchDelegate.QueryMetadata query = createQueryMetadata(queryMethod, accessor); |
| 76 | + |
| 77 | + assertThat(query.query().getLimit()).isEqualTo(10); |
| 78 | + assertThat(query.numCandidates()).isNull(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void shouldConsiderProvidedLimit() throws ReflectiveOperationException { |
| 83 | + |
| 84 | + Method method = VectorSearchRepository.class.getMethod("searchTop10ByEmbeddingNear", Vector.class, Score.class, |
| 85 | + Limit.class); |
| 86 | + |
| 87 | + MongoQueryMethod queryMethod = getMongoQueryMethod(method); |
| 88 | + MongoParametersParameterAccessor accessor = getAccessor(queryMethod, Vector.of(1, 2), Score.of(1), Limit.of(11)); |
| 89 | + |
| 90 | + VectorSearchDelegate.QueryMetadata query = createQueryMetadata(queryMethod, accessor); |
| 91 | + |
| 92 | + assertThat(query.query().getLimit()).isEqualTo(11); |
| 93 | + assertThat(query.numCandidates()).isEqualTo(11 * 20); |
| 94 | + } |
| 95 | + |
| 96 | + private VectorSearchDelegate.QueryMetadata createQueryMetadata(MongoQueryMethod queryMethod, |
| 97 | + MongoParametersParameterAccessor accessor) { |
| 98 | + |
| 99 | + VectorSearchDelegate delegate = new VectorSearchDelegate(queryMethod, converter, ValueExpressionDelegate.create()); |
| 100 | + |
| 101 | + return delegate.createQuery(mock(ValueExpressionEvaluator.class), queryMethod.getResultProcessor(), accessor, |
| 102 | + Object.class, new ParameterBindingDocumentCodec(), mock(ParameterBindingContext.class)); |
| 103 | + } |
| 104 | + |
| 105 | + private MongoQueryMethod getMongoQueryMethod(Method method) { |
| 106 | + RepositoryMetadata metadata = AnnotationRepositoryMetadata.getMetadata(method.getDeclaringClass()); |
| 107 | + return new MongoQueryMethod(method, metadata, new SpelAwareProxyProjectionFactory(), converter.getMappingContext()); |
| 108 | + } |
| 109 | + |
| 110 | + @NotNull |
| 111 | + private static MongoParametersParameterAccessor getAccessor(MongoQueryMethod queryMethod, Object... values) { |
| 112 | + return new MongoParametersParameterAccessor(queryMethod, values); |
| 113 | + } |
| 114 | + |
| 115 | + interface VectorSearchRepository extends Repository<WithVector, String> { |
| 116 | + |
| 117 | + @VectorSearch(indexName = "cos-index", searchType = VectorSearchOperation.SearchType.ANN) |
| 118 | + SearchResults<WithVector> searchTop10ByEmbeddingNear(Vector vector, Score similarity); |
| 119 | + |
| 120 | + @VectorSearch(indexName = "cos-index", searchType = VectorSearchOperation.SearchType.ENN) |
| 121 | + SearchResults<WithVector> searchTop10EnnByEmbeddingNear(Vector vector, Score similarity); |
| 122 | + |
| 123 | + @VectorSearch(indexName = "cos-index", searchType = VectorSearchOperation.SearchType.ANN) |
| 124 | + SearchResults<WithVector> searchTop10ByEmbeddingNear(Vector vector, Score similarity, Limit limit); |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + static class WithVector { |
| 129 | + |
| 130 | + Vector embedding; |
| 131 | + } |
| 132 | +} |
0 commit comments