Skip to content

Commit f3bbdb2

Browse files
rozzavbabanin
andauthored
Add BinaryVector and VectorSearchQuery vectorSearch overloads to Scala driver (#1986)
* Add BinaryVector and VectorSearchQuery vectorSearch overloads to Scala driver The Scala Aggregates wrapper only exposed the `Iterable[Double]` overload of vectorSearch. This adds the `BinaryVector` (since 5.3) and `VectorSearchQuery` auto-embedding (since 5.7) overloads. Changes the existing overload's parameter from `Iterable[java.lang.Double]` to `Iterable[Double]` to resolve Scala type inference ambiguity when multiple overloads are present. Also fixes scaladoc warnings for unresolvable cross-module Java type links. JAVA-5699 --------- Co-authored-by: Viacheslav Babanin <frest0512@gmail.com>
1 parent 966abab commit f3bbdb2

3 files changed

Lines changed: 113 additions & 6 deletions

File tree

driver-scala/src/main/scala/org/mongodb/scala/model/Aggregates.scala

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ package org.mongodb.scala.model
1818

1919
import com.mongodb.annotations.{ Beta, Reason }
2020
import com.mongodb.client.model.fill.FillOutputField
21-
import com.mongodb.client.model.search.FieldSearchPath
21+
import com.mongodb.client.model.search.{ FieldSearchPath, VectorSearchQuery }
22+
import org.bson.BinaryVector
2223

2324
import scala.collection.JavaConverters._
2425
import com.mongodb.client.model.{ Aggregates => JAggregates }
@@ -740,12 +741,71 @@ object Aggregates {
740741
*/
741742
def vectorSearch(
742743
path: FieldSearchPath,
743-
queryVector: Iterable[java.lang.Double],
744+
queryVector: Iterable[Double],
744745
index: String,
745746
limit: Long,
746747
options: VectorSearchOptions
747748
): Bson =
748-
JAggregates.vectorSearch(path, queryVector.asJava, index, limit, options)
749+
JAggregates.vectorSearch(
750+
path,
751+
queryVector.asInstanceOf[Iterable[java.lang.Double]].asJava,
752+
index,
753+
limit,
754+
options
755+
)
756+
757+
/**
758+
* Creates a `\$vectorSearch` pipeline stage supported by MongoDB Atlas.
759+
* You may use the `\$meta: "vectorSearchScore"` expression, e.g., via [[Projections.metaVectorSearchScore]],
760+
* to extract the relevance score assigned to each found document.
761+
*
762+
* @param path The field to be searched.
763+
* @param queryVector The `BinaryVector` query vector. The number of dimensions must match that of the `index`.
764+
* @param index The name of the index to use.
765+
* @param limit The limit on the number of documents produced by the pipeline stage.
766+
* @param options Optional `\$vectorSearch` pipeline stage fields.
767+
* @return The `\$vectorSearch` pipeline stage.
768+
* @see [[https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/ \$vectorSearch]]
769+
* @note Requires MongoDB 6.0.10 or greater
770+
* @since 5.8
771+
*/
772+
def vectorSearch(
773+
path: FieldSearchPath,
774+
queryVector: BinaryVector,
775+
index: String,
776+
limit: Long,
777+
options: VectorSearchOptions
778+
): Bson =
779+
JAggregates.vectorSearch(path, queryVector, index, limit, options)
780+
781+
/**
782+
* Creates a `\$vectorSearch` pipeline stage supported by MongoDB Atlas with automated embedding.
783+
* You may use the `\$meta: "vectorSearchScore"` expression, e.g., via [[Projections.metaVectorSearchScore]],
784+
* to extract the relevance score assigned to each found document.
785+
*
786+
* This overload is used for auto-embedding in Atlas. The server will automatically generate embeddings
787+
* for the query using the model specified in the index definition or via
788+
* `TextVectorSearchQuery.model`.
789+
*
790+
* @param path The field to be searched.
791+
* @param query The query specification, typically created via `VectorSearchQuery.textQuery`.
792+
* @param index The name of the index to use.
793+
* @param limit The limit on the number of documents produced by the pipeline stage.
794+
* @param options Optional `\$vectorSearch` pipeline stage fields.
795+
* @return The `\$vectorSearch` pipeline stage.
796+
* @see [[https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/ \$vectorSearch]]
797+
* @note Requires MongoDB 6.0.10 or greater
798+
* @since 5.8
799+
*/
800+
@Beta(Array(Reason.SERVER))
801+
def vectorSearch(
802+
path: FieldSearchPath,
803+
query: VectorSearchQuery,
804+
index: String,
805+
limit: Long,
806+
options: VectorSearchOptions
807+
): Bson =
808+
JAggregates.vectorSearch(path, query, index, limit, options)
749809

750810
/**
751811
* Creates a `\$rerank` pipeline stage supported by MongoDB Atlas.

driver-scala/src/main/scala/org/mongodb/scala/model/package.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ package object model {
536536
* Creates a vector field definition for a vector search index.
537537
*
538538
* @param path the field path in the document
539-
* @return a new [[com.mongodb.client.model.VectorSearchIndexFields.VectorField]]
539+
* @return a new `VectorSearchIndexFields.VectorField`
540540
*/
541541
def vectorField(path: String): com.mongodb.client.model.VectorSearchIndexFields.VectorField =
542542
com.mongodb.client.model.VectorSearchIndexFields.vectorField(path)
@@ -545,7 +545,7 @@ package object model {
545545
* Creates a filter field definition for a vector search index.
546546
*
547547
* @param path the field path in the document
548-
* @return a new [[com.mongodb.client.model.VectorSearchIndexFields.FilterField]]
548+
* @return a new `VectorSearchIndexFields.FilterField`
549549
*/
550550
def filterField(path: String): com.mongodb.client.model.VectorSearchIndexFields.FilterField =
551551
com.mongodb.client.model.VectorSearchIndexFields.filterField(path)
@@ -554,7 +554,7 @@ package object model {
554554
* Creates an auto-embed field definition for a vector search index.
555555
*
556556
* @param path the field path in the document containing the content to embed
557-
* @return a new [[com.mongodb.client.model.VectorSearchIndexFields.AutoEmbedField]]
557+
* @return a new `VectorSearchIndexFields.AutoEmbedField`
558558
*/
559559
def autoEmbedField(path: String): com.mongodb.client.model.VectorSearchIndexFields.AutoEmbedField =
560560
com.mongodb.client.model.VectorSearchIndexFields.autoEmbedField(path)

driver-scala/src/test/scala/org/mongodb/scala/model/AggregatesSpec.scala

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ import org.mongodb.scala.model.geojson.{ Point, Position }
3737
import org.mongodb.scala.model.search.SearchCount.total
3838
import org.mongodb.scala.model.search.SearchFacet.stringFacet
3939
import org.mongodb.scala.model.search.SearchHighlight.paths
40+
import com.mongodb.client.model.{ Aggregates => JAggregates }
4041
import com.mongodb.client.model.RerankQuery
42+
import com.mongodb.client.model.search.VectorSearchQuery
43+
import org.bson.BinaryVector
4144
import org.mongodb.scala.model.search.SearchCollector
4245
import org.mongodb.scala.model.search.SearchOperator.exists
4346
import org.mongodb.scala.model.search.SearchOptions.searchOptions
@@ -817,6 +820,50 @@ class AggregatesSpec extends BaseSpec {
817820
)
818821
}
819822

823+
it should "render $vectorSearch with BinaryVector" in {
824+
toBson(
825+
Aggregates.vectorSearch(
826+
fieldPath("fieldName"),
827+
BinaryVector.int8Vector(Array[Byte](0, 1, 2, 3, 4)),
828+
"indexName",
829+
1,
830+
approximateVectorSearchOptions(2)
831+
)
832+
) should equal(
833+
toBson(
834+
JAggregates.vectorSearch(
835+
fieldPath("fieldName"),
836+
BinaryVector.int8Vector(Array[Byte](0, 1, 2, 3, 4)),
837+
"indexName",
838+
1,
839+
approximateVectorSearchOptions(2)
840+
)
841+
)
842+
)
843+
}
844+
845+
it should "render $vectorSearch with VectorSearchQuery" in {
846+
toBson(
847+
Aggregates.vectorSearch(
848+
fieldPath("fieldName"),
849+
VectorSearchQuery.textQuery("sample text"),
850+
"indexName",
851+
1,
852+
approximateVectorSearchOptions(2)
853+
)
854+
) should equal(
855+
toBson(
856+
JAggregates.vectorSearch(
857+
fieldPath("fieldName"),
858+
VectorSearchQuery.textQuery("sample text"),
859+
"indexName",
860+
1,
861+
approximateVectorSearchOptions(2)
862+
)
863+
)
864+
)
865+
}
866+
820867
it should "render $rerank with single path" in {
821868
toBson(
822869
Aggregates.rerank(

0 commit comments

Comments
 (0)