DEV Community

MongoDB Guests for MongoDB

Posted on

What Is MongoDB Atlas Vector Search

This article was written by Moses Anumadu.

Traditional search relies on keywords and tags to recommend search results. This system works and is in use across multiple platforms. However, it has a major limitation that has become more apparent in recent years due to increased AI adoption. The major limitation of traditional search is that it returns search results based solely on keyword matches. Traditional searches, as we have them today, do not consider semantic meaning. For example:

Imagine that we built a movie website using traditional search. Now, if a user wants to find The Matrix, but they can't remember the name. They type in "dystopian AI reality simulation." A traditional keyword database will return zero results because those exact words are not in the movie title or description. This is the major challenge for traditional search. It returns results strictly from keywords, in this case, either in the movie name or description. So, if a user does not type the exact keywords stored in your database, the correct or intended movie does not show up, even though it is in the database. The platform simply does not know that it was the intended movie. This can be limiting and produce search results that do not accurately match the users' intent.

A better approach for this use case is to have the search not rely on keywords to recommend movies. It should recommend movies based on semantic similarities. This is a very good example of when vector search should be used over traditional search.

In this tutorial, we will explore what vector search is, its importance, what MongoDB Vector Search is, and why you should consider it for your next vector search implementation. With that said, let's get started.

Prerequisites

To follow along with this article, you should have a basic understanding of the following concepts:

  • MongoDB Atlas
  • Database and collection
  • Can perform CRUD operations
  • Basic understanding of web development technologies

What Is Vector Search?

Vector search is a search technology that uses vectors to identify semantic similarity and recommend results that are accurate in terms of meaning and not keywords.

With vector search, text, images, and other media file types are stored as embeddings. Embeddings are numerical values generated by machine learning models that capture the context, intent, and semantic similarity of the stored data. Items with similar meanings have embeddings (numeric values) that are closer.

When a search is done, the vector search identifies similar values by comparing the closeness of the content embeddings. The closer the embeddings, the more similar the contents. This returns a more accurate search result that relies on meaning over a keyword search that relies on keywords.

The major difference to note here is that keyword searches rely on keyword matches to recommend results, while vector searches rely on meaning by comparing embeddings to recommend results.

Keyword Search vs. Vector Search

Keyword Search Vector Search
Search relies on exact keyword matching to return results. Search relies on a semantic understanding of search terms.
Results are not generated based on users' intentions. They rely on keyword accuracy. Generates results with users' intent in consideration.
The use of synonyms in keyword searches does not often return the best search results. Eg, car and vehicle won't return the same result. Synonyms in vector search generate far more accurate results. It factors in the actual meaning of the words to generate results.
Keyword search queries are not flexible to things like typos and long search queries. Search keywords or phrases can be sentences. It can be flexible with misspelled words and still return accurate results.

How Embeddings Enable Vector Search

What Is an Embedding?

Think of embeddings as the foundation of vector searches. Embeddings are numerical values generated by artificial intelligence. These numeric values represent the meaning of text and media files, so vector search can compare them and return more accurate, similar results.

In a keyword search query, a traditional database does not treat words as symbols with meaning; it sees them as independent flat characters that don't have links to the sentence's meaning. So it only returns results that match exactly. An example can be a search keyword of "CAT" would return a CAT. Not because it understands it, but because the keywords match. But a search keyword of VEHICLE would not return a result of CAR.

How AI generates these similarities

Let's take a moment to examine how an AI embedding model generates the numbers in an embedding. Imagine you need to describe an apple using a scale of 1-10. Is an apple sweet? Yes. On a scale of 1 - 10, how sweet is an apple? Say 8. Is an apple crunchy? Yes. On a scale of 1 - 10, how crunchy is an apple? Say 6. Is an apple red in color? Yes. On a scale of 1 to 10, how red is an apple? Say 9. This would give us an embedding of [8, 6, 9]. A fruit like a banana can be [9, 1, 0].

The AI embedding model does something very similar to the example above; when it processes a sentence, it outputs a long list of numbers (called a vector). This list serves as a precise map coordinate for that specific piece of text in a giant, digital "map of meaning."
This results in words with similar meaning having vectors that are closely related or similar.

Why Embeddings Matter

Embeddings are like the building blocks of vectors. With them, vectors are generated, and similarity in terms of meaning can be measured in a search query.

How MongoDB Vector Search Works

The Vector Search Workflow

In a MongoDB vector search implementation, data is stored together with its generated vector in the same collection. This simplifies the implementation when compared to the steps required to implement this using different technologies. Let's examine the workflow for both storing a document and retrieving it during a search query.

Storing document

Storing record

This process starts with storing our normal documents in a MongoDB collection. A document can be in text or media format.

Embeddings are generated

After storing this document, embeddings are generated using any of the AI models that have this compatibility. Some popular examples are VoyageAI, OpenAI, Cohere, and Hugging Face.

Documents are updated to contain vectors.

The data collection in Atlas is updated to contain the generated vectors. This saves the vector as a field in the collection. Keeping both the data and its embeddings in one collection.

A vector search index is created

The final step in this flow is creating the vector search index. This boosts performance during searches.

The image below provides a visual explanation of the flow;

Search workflow

The MongoDB vector search workflow consists of a few steps; it starts with a user query. The query is converted into embeddings using an AI embedding model (for best results, use the same embedding model as the one used to embed the documents in the database is advised).

After the vectors are generated, MongoDB vector search calculates the semantic proximity between the search query vector and the vector of the documents stored in the collection. It then returns the results, which are semantically matching items to the user search query. The image below provides a more visual explanation of the process.

Common Use Cases

There is no doubt about the importance of vector searches. However, this does not make it the right tool for every search implementation. Vector search implementations require AI models to generate vectors. This can make the search implementation a bit more complex and costly. With that in mind, it is important to explore some real-world use cases where it can be a good fit and also where it is not. But in general, vector searches are a great solution for search results that want semantic similarity. Some examples include:

  • Semantic document search
  • AI knowledge bases
  • Product recommendation systems
  • Similar content discovery
  • FAQ search
  • AI assistants using business data

Benefits and Considerations

Benefits

Some of the key benefits include:

More relevant search results

Using vectors to measure similarity enables vector searches to return more relevant results. Unlike keyword searches, vector search can provide relevant results even if the search query contains misspelled or slang words.

Better user experience

Vector search implementation can improve the user experience. Vector search is less rigid; it provides some flexibility for users when typing their search. This improves the users' experience of the application.

Natural language querying

Vector search implementation allows users to type search queries in natural language, rather than having to target keywords, often explaining the expected result rather than targeting the keywords.

Native integration within MongoDB

Vector Search is part of the core features of MongoDB. This means that it is already integrated with your database and collections. If you have an already existing collection in MongoDB, you can easily use your existing data to implement vector searching.

Reduced need for a dedicated vector database

With MongoDB, you do not need a separate vector database. Data can be stored alongside its vector embeddings in the MongoDB collection.

Considerations

While vector search can provide more accurate results, it is worth noting that it is not a complete replacement for traditional searches. The following key points are worth noting before deciding to implement a vector search in an application:

The need for embedding models:

Vector search implementation requires the use of AI models to generate embeddings. These models can be expensive and provide an additional configuration layer.

Additional storage requirements for vectors

Vectors can be large in size. The vector for a sentence can have up to 8,192 dimensions. Imagine when you have a hundred blog posts. This can take up a huge amount of space and can lead to higher cloud bills.

Choosing an appropriate embedding model

There are several embedding models that are great for different industries. Choosing the right model for your particular use case is a factor that should be considered. For example, a model that was designed for the fashion industry might not give the best result for a model optimized for medical research.

When keyword search may still be the better choice

Vector search is not a complete replacement for keyword search. Keyword search can still be more effective for exact match searches. An example can be searching for an exact serial number, username, or any other situation that returns an exact keyword result.

When Should You Use MongoDB Vector Search?

A vector search implementation that does not use MongoDB requires data, which can be in the form of text, images, or audio, to be stored in a traditional database; the generated vector for the items will be stored in a different dedicated vector database. This results in repetition because two databases are used, increasing the complexity of keeping the two databases in sync and fragmenting data (text and vectors).

MongoDB vector search bridges that gap. It lets data and their generated vectors be stored in the same collection.

Another major advantage that MongoDB vector search brings is that it keeps data in sync. A very common issue with vector search integrations where one database is used for data and another for storing vectors is that the data is often not in sync. If a record is deleted from the regular database and the vector is not, search results can be recommended that are no longer available in the database. MongoDB vector search stores the record and keeps it in sync. If a record is deleted from its collection, its vector is deleted as well, keeping both the record and its vector in sync.

MongoDB vector search eliminates the need for new infrastructure by storing both records and vectors in the same collection.

Conclusion

In this article, we explored what vector search is, how it works, how AI comes into the picture, and what differentiates it from traditional keyword search. We also took it a step further and explored how MongoDB vector search works and what advantages it has over other vector databases.

By the end of this article, you should have a basic understanding of how:

  • Vector search retrieves information based on meaning rather than exact keywords.
  • Embeddings make semantic search possible.
  • MongoDB provides native vector search capabilities that integrate with your existing data.
  • Understanding these fundamentals is the first step toward building intelligent search experiences.

For a deeper dive on how to implement vector search with MongoDB, you can check out the MongoDB documentation here. If you prefer a step-by-step tutorial, check out this tutorial here.

Now you have the basics; go build something great. Cheers.

Top comments (0)