|
| 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; |
| 17 | + |
| 18 | +import java.lang.annotation.Documented; |
| 19 | +import java.lang.annotation.ElementType; |
| 20 | +import java.lang.annotation.Retention; |
| 21 | +import java.lang.annotation.RetentionPolicy; |
| 22 | +import java.lang.annotation.Target; |
| 23 | + |
| 24 | +import org.springframework.core.annotation.AliasFor; |
| 25 | +import org.springframework.data.mongodb.core.aggregation.VectorSearchOperation; |
| 26 | + |
| 27 | +/** |
| 28 | + * Annotation to declare Vector Search queries directly on repository methods. Vector Search queries are used to search |
| 29 | + * for similar documents based on vector embeddings typically returning |
| 30 | + * {@link org.springframework.data.domain.SearchResults} and limited by either a |
| 31 | + * {@link org.springframework.data.domain.Score} (within) or a {@link org.springframework.data.domain.Range} of scores |
| 32 | + * (between). |
| 33 | + * <p> |
| 34 | + * Vector search must define an index name using the {@link #indexName()} attribute. The index must be created in the |
| 35 | + * MongoDB Atlas cluster before executing the query. Any misspelling of the index name will result in returning no |
| 36 | + * results. |
| 37 | + * <p> |
| 38 | + * When using pre-filters, you can either define {@link #filter()} or use query derivation to define the pre-filter. |
| 39 | + * {@link org.springframework.data.domain.Vector} and distance parameters are considered once these are present. Vector |
| 40 | + * search supports sorting and will consider {@link org.springframework.data.domain.Sort} parameters. |
| 41 | + * |
| 42 | + * @author Mark Paluch |
| 43 | + * @since 5.0 |
| 44 | + * @see org.springframework.data.domain.Score |
| 45 | + * @see org.springframework.data.domain.Vector |
| 46 | + * @see org.springframework.data.domain.SearchResults |
| 47 | + */ |
| 48 | +@Retention(RetentionPolicy.RUNTIME) |
| 49 | +@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE }) |
| 50 | +@Documented |
| 51 | +@Query |
| 52 | +@Hint |
| 53 | +public @interface VectorSearch { |
| 54 | + |
| 55 | + /** |
| 56 | + * Configuration whether to use |
| 57 | + * {@link org.springframework.data.mongodb.core.aggregation.VectorSearchOperation.SearchType#ANN} or |
| 58 | + * {@link org.springframework.data.mongodb.core.aggregation.VectorSearchOperation.SearchType#ENN} for the search. |
| 59 | + * |
| 60 | + * @return the search type to use. |
| 61 | + */ |
| 62 | + VectorSearchOperation.SearchType searchType() default VectorSearchOperation.SearchType.DEFAULT; |
| 63 | + |
| 64 | + /** |
| 65 | + * Name of the Atlas Vector Search index to use. Atlas Vector Search doesn't return results if you misspell the index |
| 66 | + * name or if the specified index doesn't already exist on the cluster. |
| 67 | + * |
| 68 | + * @return name of the Atlas Vector Search index to use. |
| 69 | + */ |
| 70 | + @AliasFor(annotation = Hint.class, value = "indexName") |
| 71 | + String indexName(); |
| 72 | + |
| 73 | + /** |
| 74 | + * Indexed vector type field to search. This is defaulted from the domain model using the first Vector property found. |
| 75 | + * |
| 76 | + * @return an empty String by default. |
| 77 | + */ |
| 78 | + String path() default ""; |
| 79 | + |
| 80 | + /** |
| 81 | + * Takes a MongoDB JSON (MQL) string defining the pre-filter against indexed fields. Supports Value Expressions. Alias |
| 82 | + * for {@link VectorSearch#filter}. |
| 83 | + * |
| 84 | + * @return an empty String by default. |
| 85 | + */ |
| 86 | + @AliasFor(annotation = Query.class) |
| 87 | + String value() default ""; |
| 88 | + |
| 89 | + /** |
| 90 | + * Takes a MongoDB JSON (MQL) string defining the pre-filter against indexed fields. Supports Value Expressions. Alias |
| 91 | + * for {@link VectorSearch#value}. |
| 92 | + * |
| 93 | + * @return an empty String by default. |
| 94 | + */ |
| 95 | + @AliasFor(annotation = Query.class, value = "value") |
| 96 | + String filter() default ""; |
| 97 | + |
| 98 | + /** |
| 99 | + * Number of documents to return in the results. This value can't exceed the value of {@link #numCandidates} if you |
| 100 | + * specify {@link #numCandidates}. Limit accepts Value Expressions. A Vector Search method cannot define both, |
| 101 | + * {@code limit()} and a {@link org.springframework.data.domain.Limit} parameter. Supports Value Expressions. |
| 102 | + * |
| 103 | + * @return number of documents to return in the results. |
| 104 | + */ |
| 105 | + String limit() default ""; |
| 106 | + |
| 107 | + /** |
| 108 | + * Number of nearest neighbors to use during the search. Value must be less than or equal to ({@code <=}) |
| 109 | + * {@code 10000}. You can't specify a number less than the {@link #limit() number of documents to return}. We |
| 110 | + * recommend that you specify a number at least {@code 20} times higher than the {@link #limit() number of documents |
| 111 | + * to return} to increase accuracy. |
| 112 | + * <p> |
| 113 | + * This over-request pattern is the recommended way to trade off latency and recall in your ANN searches, and we |
| 114 | + * recommend tuning this parameter based on your specific dataset size and query requirements. Required if the query |
| 115 | + * uses |
| 116 | + * {@link org.springframework.data.mongodb.core.aggregation.VectorSearchOperation.SearchType#ANN}/{@link org.springframework.data.mongodb.core.aggregation.VectorSearchOperation.SearchType#DEFAULT}. |
| 117 | + * Supports Value Expressions. |
| 118 | + * |
| 119 | + * @return number of nearest neighbors to use during the search. |
| 120 | + */ |
| 121 | + String numCandidates() default ""; |
| 122 | + |
| 123 | +} |
0 commit comments