If you have ever used Amazon or Netflix, you know what a great search experience feels like. If you search for “shos”, the engine knows you meant “shoes” and returns the correct products instantly.
Building a search engine that tolerates typos (fuzzy matching) and understands the root of words (stemming) is incredibly difficult. Historically, developers had to use their primary database (like MongoDB or PostgreSQL) to store data, and then build complex pipelines to copy all that data over to a dedicated search database like Elasticsearch. This resulted in data sync issues, doubled hardware costs, and massive engineering headaches.
Atlas Search solves this. It integrates a world-class search engine directly into your MongoDB cluster.
What is Atlas Search?
Atlas Search is a fully managed, full-text search engine built directly on top of Apache Lucene.
Apache Lucene is the open-source search library that powers almost every major search engine in the world (including Elasticsearch and Solr). MongoDB Atlas took the Lucene engine and embedded it seamlessly into the MongoDB architecture.
When you insert a document into your MongoDB collection, Atlas automatically syncs that document to the embedded Lucene index. You can then query it using a special aggregation stage called $search.
Standard Text Indexes vs Atlas Search
In Module 8, we learned how to create a standard text index. If standard text indexes exist, why do we need Atlas Search?
Standard Text Indexes:
- Great for exact word matches.
- Extremely limited language support.
- No tolerance for typos. If you search for
appple, it will return zero results forapple. - No support for “Autocomplete” (search-as-you-type).
Atlas Search (Lucene):
- Fuzzy Matching: Understands typos and misspellings.
- Analyzers: Understands linguistics (e.g., searching for “running” will also find “run” and “ran”).
- Autocomplete: Can predict what the user is typing in real-time.
- Scoring: Ranks results by relevance so the best match always appears at the top.
Creating a Search Index
Before you can run a $search query, you must create a Search Index. You do this visually from the Atlas Dashboard.
- Log in to your Atlas Dashboard.
- Click on Search in the left sidebar.
- Click Create Search Index.
- Select the Visual Editor.
- Choose your database and collection (e.g.,
ecommerce->products). - Give your index a name (e.g.,
default). - Click Create Index.
By default, Atlas creates a “Dynamic” index, which automatically indexes every single string field in every document in that collection. This is great for getting started quickly!
The $search Aggregation Stage
Atlas Search queries are executed using the Aggregation Framework. The $search stage must be the absolute first stage in your pipeline.
Here is a basic search query that searches the description field for the word “laptop”:
db.products.aggregate([
{
$search: {
index: "default", // The name of the index you created in the UI
text: {
query: "laptop",
path: "description" // Which field to search in
}
}
},
{ $limit: 10 }
])
Note: Because this uses Apache Lucene, it will automatically find documents containing “laptops” (plural) because of stemming!
Fuzzy Matching (Typo Tolerance)
Fuzzy matching is what makes a search engine feel “smart.” It allows the engine to return results even if the user makes a spelling mistake.
You enable this by adding a fuzzy object to your text query.
db.products.aggregate([
{
$search: {
index: "default",
text: {
query: "MacBok", // Typo! Missing the 'o'
path: "name",
fuzzy: {
maxEdits: 2 // Allow up to 2 characters to be wrong
}
}
}
}
])
Even though the user typed “MacBok”, this query will successfully find and return “MacBook”!
Autocomplete (Search-As-You-Type)
When a user starts typing in a search bar, you want to show them instant suggestions in a dropdown menu. This is called Autocomplete.
To use Autocomplete, you cannot use the “Dynamic” index. You must configure your Search Index in the Atlas UI to explicitly map a specific field as an autocomplete type.
Once the index is configured, the query is incredibly simple:
db.products.aggregate([
{
$search: {
index: "autocomplete_index",
autocomplete: {
query: "sam", // The user has only typed 'sam' so far
path: "name",
fuzzy: { maxEdits: 1 } // Autocomplete with typo tolerance!
}
}
},
{ $limit: 5 },
{ $project: { name: 1, _id: 0 } } // Only return the names for the dropdown
])
This will instantly return suggestions like “Samsung”, “Samsonite”, or “Samurai Sword”.
Highlighting Results
If you search for a word inside a massive 10,000-word blog post, how does the user know where the word actually appeared? You use Highlighting.
Atlas Search can automatically wrap the matching words in HTML tags (like <em>) so you can easily highlight them yellow on your website.
db.articles.aggregate([
{
$search: {
index: "default",
text: { query: "database", path: "body" },
highlight: { path: "body" } // Request highlighting on the body field
}
},
{
$project: {
title: 1,
highlights: { $meta: "searchHighlights" } // Extract the highlighted snippets!
}
}
])
Summary
Atlas Search eliminates the need to manage a separate Elasticsearch cluster.
- It is powered by Apache Lucene, the industry standard for search.
- The
$searchstage brings advanced capabilities directly into the Aggregation Pipeline. - Fuzzy Matching effortlessly handles typos.
- Autocomplete provides instant, real-time search suggestions.
- Highlighting extracts exact snippets showing where the match occurred.
We are now approaching the end of our journey! In Module 24, we will explore one of the most exciting real-time features of MongoDB: Change Streams!
Discussion
Loading comments...