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