|
| 1 | +package org.springframework.ai.vectorstore.milvus; |
| 2 | + |
| 3 | +import org.springframework.ai.vectorstore.SearchRequest; |
| 4 | +import org.springframework.ai.vectorstore.filter.Filter; |
| 5 | +import org.springframework.lang.Nullable; |
| 6 | + |
| 7 | +/** |
| 8 | + * A specialized {@link SearchRequest} for Milvus vector search, extending the base |
| 9 | + * request with Milvus-specific parameters. |
| 10 | + * <p> |
| 11 | + * This class introduces two additional fields: |
| 12 | + * <ul> |
| 13 | + * <li>{@code nativeExpression} - A native Milvus filter expression (e.g., |
| 14 | + * {@code "city LIKE |
| 15 | + * 'New%'"}).</li> |
| 16 | + * <li>{@code searchParamsJson} - A JSON string containing search parameters (e.g., |
| 17 | + * {@code "{\"nprobe\":128}"}).</li> |
| 18 | + * </ul> |
| 19 | + * <p> |
| 20 | + * Use the {@link MilvusBuilder} to construct instances of this class. |
| 21 | + * |
| 22 | + * @author waileong |
| 23 | + */ |
| 24 | +public final class MilvusSearchRequest extends SearchRequest { |
| 25 | + |
| 26 | + @Nullable |
| 27 | + private final String nativeExpression; |
| 28 | + |
| 29 | + @Nullable |
| 30 | + private final String searchParamsJson; |
| 31 | + |
| 32 | + /** |
| 33 | + * Private constructor to initialize a MilvusSearchRequest using the base request and |
| 34 | + * builder. |
| 35 | + * @param baseRequest The base {@link SearchRequest} containing standard search |
| 36 | + * fields. |
| 37 | + * @param builder The {@link MilvusBuilder} containing Milvus-specific parameters. |
| 38 | + */ |
| 39 | + private MilvusSearchRequest(SearchRequest baseRequest, MilvusBuilder builder) { |
| 40 | + super(baseRequest); // Copy all standard fields |
| 41 | + this.nativeExpression = builder.nativeExpression; |
| 42 | + this.searchParamsJson = builder.searchParamsJson; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Retrieves the native Milvus filter expression. |
| 47 | + * @return A string representing the native Milvus expression, or {@code null} if not |
| 48 | + * set. |
| 49 | + */ |
| 50 | + @Nullable |
| 51 | + public String getNativeExpression() { |
| 52 | + return this.nativeExpression; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Retrieves the JSON-encoded search parameters. |
| 57 | + * @return A JSON string containing search parameters, or {@code null} if not set. |
| 58 | + */ |
| 59 | + @Nullable |
| 60 | + public String getSearchParamsJson() { |
| 61 | + return this.searchParamsJson; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Creates a new {@link MilvusBuilder} for constructing a {@link MilvusSearchRequest}. |
| 66 | + * @return A new {@link MilvusBuilder} instance. |
| 67 | + */ |
| 68 | + public static MilvusBuilder milvusBuilder() { |
| 69 | + return new MilvusBuilder(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Builder class for constructing instances of {@link MilvusSearchRequest}. |
| 74 | + */ |
| 75 | + public static class MilvusBuilder { |
| 76 | + |
| 77 | + private final SearchRequest.Builder baseBuilder = SearchRequest.builder(); |
| 78 | + |
| 79 | + @Nullable |
| 80 | + private String nativeExpression; |
| 81 | + |
| 82 | + @Nullable |
| 83 | + private String searchParamsJson; |
| 84 | + |
| 85 | + /** |
| 86 | + * {@link Builder#query(java.lang.String)} |
| 87 | + */ |
| 88 | + public MilvusBuilder query(String query) { |
| 89 | + this.baseBuilder.query(query); |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * {@link Builder#topK(int)} |
| 95 | + */ |
| 96 | + public MilvusBuilder topK(int topK) { |
| 97 | + this.baseBuilder.topK(topK); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * {@link Builder#similarityThreshold(double)} |
| 103 | + */ |
| 104 | + public MilvusBuilder similarityThreshold(double threshold) { |
| 105 | + this.baseBuilder.similarityThreshold(threshold); |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * {@link Builder#similarityThresholdAll()} |
| 111 | + */ |
| 112 | + public MilvusBuilder similarityThresholdAll() { |
| 113 | + this.baseBuilder.similarityThresholdAll(); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * {@link Builder#filterExpression(String)} |
| 119 | + */ |
| 120 | + public MilvusBuilder filterExpression(String textExpression) { |
| 121 | + this.baseBuilder.filterExpression(textExpression); |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * {@link Builder#filterExpression(Filter.Expression)} |
| 127 | + */ |
| 128 | + public MilvusBuilder filterExpression(Filter.Expression expression) { |
| 129 | + this.baseBuilder.filterExpression(expression); |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Sets the native Milvus filter expression. |
| 135 | + * @param nativeExpression The native Milvus expression string. |
| 136 | + * @return This builder instance. |
| 137 | + */ |
| 138 | + public MilvusBuilder nativeExpression(String nativeExpression) { |
| 139 | + this.nativeExpression = nativeExpression; |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Sets the JSON-encoded search parameters. |
| 145 | + * @param searchParamsJson A JSON string containing search parameters. |
| 146 | + * @return This builder instance. |
| 147 | + */ |
| 148 | + public MilvusBuilder searchParamsJson(String searchParamsJson) { |
| 149 | + this.searchParamsJson = searchParamsJson; |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Builds and returns a {@link MilvusSearchRequest} instance. |
| 155 | + * @return A new {@link MilvusSearchRequest} object with the specified parameters. |
| 156 | + */ |
| 157 | + public MilvusSearchRequest build() { |
| 158 | + SearchRequest parentRequest = this.baseBuilder.build(); |
| 159 | + return new MilvusSearchRequest(parentRequest, this); |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments