A Comprehensive Guide to MongoDB Vector Search with Examples

What is Vector Search?

Vector search involves finding similar items based on vector representations of data. Vectors are numerical representations of objects, often derived from machine learning models. 

By comparing vectors, we can determine the similarity between objects, making it a powerful tool for various applications.

For instance, you watched a movie last week and wanted to suggest it to your friend, but you don’t recall the title and the cast as they’re all new. You vaguely remember an action scene where the protagonist is holding a yellow umbrella but still want to find the movie title. We often face such situations and we build many such Applications with those use cases — MongoDB Atlas Vector Search is the solution for all such use cases.

With Vector Search, users can enable use cases like : 

 

  • Semantic search
  • Synonym generation
  • Feature extraction
  • Image search
  • Recommendations & relevance scoring
  • Questions and answer systems
  • RAG (retrieval-augmented generation)

Setting Up MongoDB for Vector Search

Click on JSON Editor under ‘Atlas Vector Search’

Example : 


```{
"fields":[
{
"type": "vector",
"path": "<field-to-index>",
"numDimensions": <number-of-dimensions>,
"similarity": "euclidean | cosine | dotProduct"
},
{
"type": "filter",
"path": "<field-to-index>"
},
...
]
}

Example :

db.embedded_movies.createSearchIndex(
"vector_index", 
"vectorSearch", 
{
"fields": [
{
"type": "vector",
"path": "plot_embedding",
"numDimensions": 1536,
"similarity": "euclidean"
}
]
}
);

```

 

 

You’re All Set!

Your search index build is now in progress. You will receive an alert once it is complete.

Indexes may take a few minutes or longer to build, depending on the size of your collection. You can see the status of your index in the Atlas Search tab.

With everything in place, you can now query your data using the $vectorSearch stage in the

MongoDB Query Language workflow to retrieve results that align with your search intent.

 

Example query

db.embedded_movies.aggregate([
{
"$vectorSearch": {
"index": "vector_index",
"path": "plot_embedding",
"queryVector": [-0.0016261312,-0.028070757,-0.011342932, ………………………… ,-0.011649498,-0.011982721,-0.016967745,-0.0060913274,-0.007130985,-0.013109017,-0.009710136],
"numCandidates": 150,
"limit": 10
}
},
{
"$project": {
"_id": 0,
"plot": 1,
"title": 1,
"score": { $meta: "vectorSearchScore" }
}
}
])

 

The above query uses the $vectorSearch stage to search the plot_embedding field using vector embeddings for the string time travel. It considers up to 150 nearest neighbors, and returns 10 documents in the results. The query also specifies a $project stage to do the following:

Exclude the _id field and include only the plot and title fields in the results.

Add a field named score that shows the vector search score for each document in the results.

Advantages of Vector Search

  1. Efficiency: Vector search allows for the rapid retrieval of similar items from extensive datasets.
  2. Flexibility: It is adaptable to various data types, including text, images, and audio.
  3. Accuracy: Advanced models like BERT ensure high precision in capturing the semantic meaning of descriptions.

Conclusion

MongoDB vector search is an effective tool for building applications requiring similarity search. By utilizing pre-trained models like BERT, you can effortlessly convert data into vectors and perform efficient searches.

What Does Vector Search Entail?

Vector search is a technique enabling semantic search, querying data based on its inherent meaning. It employs machine learning models, or encoders, to transform various data forms—such as text, audio, and images—into high-dimensional vectors. These vectors encapsulate the semantic essence of the data, allowing for the identification of similar content based on vector proximity in a high-dimensional space.

Vectorized search complements traditional keyword-based methods and enhances large language models (LLMs) by providing ground truth beyond their inherent knowledge. In search scenarios, vector search retrieves relevant results even without exact phrasing, benefiting areas like natural language processing and recommendation systems.

Consider a MongoDB collection of blog articles where you want to find documents related to artificial intelligence. A full-text search would return documents containing the exact phrase “artificial intelligence” or “AI,” but might include irrelevant mentions. Vector search, however, transforms both the query and documents into numerical vectors using techniques like word or document embeddings. These vectors are compared using a cosine similarity metric, ensuring more accurate, contextually relevant results focused on AI, rather than mere mentions.

What is Atlas Vector Search?

MongoDB’s Atlas platform offers a fully managed vector search feature, integrating the operational database and a vector store. This unified approach supports quick integrations into LLMs, facilitating the development of semantic search and AI-powered applications using MongoDB-stored data.

Semantic Search and Vectors

Semantic search focuses on the meaning of data rather than the data itself. Vectors are numeric representations of data, searchable through advanced machine learning algorithms.

Atlas Vector Search and Atlas Search have distinct functionalities. Atlas Vector Search enables semantic searches using vectors’ meanings, while Atlas Search facilitates keyword searches based on text and predefined synonym mappings.

KNN, ANN, and Vector Embeddings

K-nearest neighbors (kNN) algorithms locate nearby vectors. Approximate nearest neighbors (ANNs) find similar vectors, trading some accuracy for performance, using hierarchical navigable small world (HNSW) graphs. Atlas Vector Search supports embeddings from any provider within a 2,048-dimension width limit, functioning as a vector store in frameworks like LangChain and LlamaIndex.

Atlas Vector Search can query any data convertible to embeddings, including images and media files. The document model allows embedding storage alongside rich data within documents.

  • Ask Question