In this guide, you can learn how to use the Atlas Vector Search feature in the Kotlin driver. The Aggregates
builders class provides the the vectorSearch()
helper method that you can use to create a $vectorSearch pipeline stage. This pipeline stage allows you to perform a semantic search on your documents. A semantic search is a type of search which locates information that is similar in meaning, but not necessarily identical, to your provided search term or phrase.
To learn what versions of MongoDB Atlas support this feature, see Limitations in the MongoDB Atlas documentation.
To use this feature, you must create a vector search index and index your vector embeddings. To learn about how to programmatically create a vector search index, see the Atlas Search and Vector Search Indexes section in the Indexes guide. To learn more about vector embeddings, see How to Index Vector Embeddings for Vector Search in the Atlas documentation.
After you create a vector search index on your vector embeddings, you can reference this index in your pipeline stage, as shown in the following section.
The example in this section uses data modeled with the following Kotlin data class:
data class MovieAlt( val title: String, val year: Int, val plot: String, val plotEmbedding: List<Double>)
This example shows how to build an aggregation pipeline that uses the vectorSearch()
method to perform an exact vector search with the following specifications:
Searches plotEmbedding
field values by using vector embeddings of a string value
Uses the mflix_movies_embedding_index
vector search index
Returns 1 document
Filters for documents in which the year
value is at least 2016
Aggregates.vectorSearch( SearchPath.fieldPath(MovieAlt::plotEmbedding.name), BinaryVector.floatVector(floatArrayOf(0.0001f, 1.12345f, 2.23456f, 3.34567f, 4.45678f)), "mflix_movies_embedding_index", 1.toLong(), exactVectorSearchOptions().filter(Filters.gte(MovieAlt::year.name, 2016)))
Tip Query Vector Type
The preceding example creates an instance of BinaryVector
to serve as the query vector, but you can also create a List
of Double
instances. However, we recommend that you use the BinaryVector
type to improve storage efficiency.
Visit the Atlas documentation to find more tutorials on using the Kotlin driver to perform Atlas Vector Searches.
To learn more about the methods and types mentioned in this guide, see the following API documentation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4