|
37 | 37 | import org.bson.BsonType;
|
38 | 38 | import org.bson.BsonValue;
|
39 | 39 | import org.bson.Document;
|
| 40 | +import org.bson.Vector; |
40 | 41 | import org.bson.codecs.configuration.CodecRegistry;
|
41 | 42 | import org.bson.conversions.Bson;
|
42 | 43 |
|
@@ -963,28 +964,37 @@ public static Bson vectorSearch(
|
963 | 964 | notNull("queryVector", queryVector);
|
964 | 965 | notNull("index", index);
|
965 | 966 | notNull("options", options);
|
966 |
| - return new Bson() { |
967 |
| - @Override |
968 |
| - public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) { |
969 |
| - Document specificationDoc = new Document("path", path.toValue()) |
970 |
| - .append("queryVector", queryVector) |
971 |
| - .append("index", index) |
972 |
| - .append("limit", limit); |
973 |
| - specificationDoc.putAll(options.toBsonDocument(documentClass, codecRegistry)); |
974 |
| - return new Document("$vectorSearch", specificationDoc).toBsonDocument(documentClass, codecRegistry); |
975 |
| - } |
| 967 | + return new VectorSearchBson(path, queryVector, index, limit, options); |
| 968 | + } |
976 | 969 |
|
977 |
| - @Override |
978 |
| - public String toString() { |
979 |
| - return "Stage{name=$vectorSearch" |
980 |
| - + ", path=" + path |
981 |
| - + ", queryVector=" + queryVector |
982 |
| - + ", index=" + index |
983 |
| - + ", limit=" + limit |
984 |
| - + ", options=" + options |
985 |
| - + '}'; |
986 |
| - } |
987 |
| - }; |
| 970 | + /** |
| 971 | + * Creates a {@code $vectorSearch} pipeline stage supported by MongoDB Atlas. |
| 972 | + * You may use the {@code $meta: "vectorSearchScore"} expression, e.g., via {@link Projections#metaVectorSearchScore(String)}, |
| 973 | + * to extract the relevance score assigned to each found document. |
| 974 | + * |
| 975 | + * @param queryVector The {@linkplain Vector query vector}. The number of dimensions must match that of the {@code index}. |
| 976 | + * @param path The field to be searched. |
| 977 | + * @param index The name of the index to use. |
| 978 | + * @param limit The limit on the number of documents produced by the pipeline stage. |
| 979 | + * @param options Optional {@code $vectorSearch} pipeline stage fields. |
| 980 | + * @return The {@code $vectorSearch} pipeline stage. |
| 981 | + * @mongodb.atlas.manual atlas-vector-search/vector-search-stage/ $vectorSearch |
| 982 | + * @mongodb.atlas.manual atlas-search/scoring/ Scoring |
| 983 | + * @mongodb.server.release 6.0 |
| 984 | + * @see Vector |
| 985 | + * @since 5.3 |
| 986 | + */ |
| 987 | + public static Bson vectorSearch( |
| 988 | + final FieldSearchPath path, |
| 989 | + final Vector queryVector, |
| 990 | + final String index, |
| 991 | + final long limit, |
| 992 | + final VectorSearchOptions options) { |
| 993 | + notNull("path", path); |
| 994 | + notNull("queryVector", queryVector); |
| 995 | + notNull("index", index); |
| 996 | + notNull("options", options); |
| 997 | + return new VectorSearchBson(path, queryVector, index, limit, options); |
988 | 998 | }
|
989 | 999 |
|
990 | 1000 | /**
|
@@ -2145,6 +2155,45 @@ public String toString() {
|
2145 | 2155 | }
|
2146 | 2156 | }
|
2147 | 2157 |
|
| 2158 | + private static class VectorSearchBson implements Bson { |
| 2159 | + private final FieldSearchPath path; |
| 2160 | + private final Object queryVector; |
| 2161 | + private final String index; |
| 2162 | + private final long limit; |
| 2163 | + private final VectorSearchOptions options; |
| 2164 | + |
| 2165 | + VectorSearchBson(final FieldSearchPath path, final Object queryVector, |
| 2166 | + final String index, final long limit, |
| 2167 | + final VectorSearchOptions options) { |
| 2168 | + this.path = path; |
| 2169 | + this.queryVector = queryVector; |
| 2170 | + this.index = index; |
| 2171 | + this.limit = limit; |
| 2172 | + this.options = options; |
| 2173 | + } |
| 2174 | + |
| 2175 | + @Override |
| 2176 | + public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) { |
| 2177 | + Document specificationDoc = new Document("path", path.toValue()) |
| 2178 | + .append("queryVector", queryVector) |
| 2179 | + .append("index", index) |
| 2180 | + .append("limit", limit); |
| 2181 | + specificationDoc.putAll(options.toBsonDocument(documentClass, codecRegistry)); |
| 2182 | + return new Document("$vectorSearch", specificationDoc).toBsonDocument(documentClass, codecRegistry); |
| 2183 | + } |
| 2184 | + |
| 2185 | + @Override |
| 2186 | + public String toString() { |
| 2187 | + return "Stage{name=$vectorSearch" |
| 2188 | + + ", path=" + path |
| 2189 | + + ", queryVector=" + queryVector |
| 2190 | + + ", index=" + index |
| 2191 | + + ", limit=" + limit |
| 2192 | + + ", options=" + options |
| 2193 | + + '}'; |
| 2194 | + } |
| 2195 | + } |
| 2196 | + |
2148 | 2197 | private Aggregates() {
|
2149 | 2198 | }
|
2150 | 2199 | }
|
0 commit comments