Learn the basics of MongoDB Search, a powerful lexical search service built directly into MongoDB Atlas. Learn how to build sophisticated, fast, and relevant search experiences without managing separate infrastructure.
We'll cover everything you need to get started, from the fundamental concepts to practical implementation. You will learn: How MongoDB Search works under the hood using Apache Lucene The process of indexing, tokenization, and using analyzers How to create your first search index in the MongoDB Shell How to query your data using the aggregation pipeline and the $search stage How MongoDB Search calculates relevance scoring (BM25) to return the best results first
Chapters: 00:00:00 What is MongoDB Search? 00:00:28 Common Use Cases for MongoDB Search 00:00:59 The Problem with Traditional Regex Queries 00:01:40 How MongoDB Search & Apache Lucene Work 00:02:32 The Indexing Process: Tokenization & Analyzers 00:02:58 How to Create a Search Index 00:03:51 How to Run a Search Query (Aggregation) 00:05:16 How Relevance Scoring Works (BM25) 00:06:02 Managing Search Indexes (Shell & UI) 00:06:51 Lesson Recap
AI Generated·May contain errors
Video Summary
AI Generated
MongoDB Atlas Search is a lexical search service integrated into MongoDB Atlas that enables sophisticated full-text search capabilities without requiring separate infrastructure. By leveraging Apache Lucene, it provides efficient keyword searching, autocomplete, and fuzzy search, overcoming the performance and functional limitations of traditional regex queries.
Ever felt the frustration of a search that just doesn't get what you're looking for?
MongoDB Atlas Search is a lexical search service that allows you to create sophisticated search
experiences without managing separate infrastructure. But to fully leverage the
capabilities of Atlas Search, we first need to understand how it works. In this video,
we'll explore what Atlas Search is, provide an overview of how it works under the hood,
and examine some key use cases. Let's get started. Atlas Search is most useful for
scenarios where users need to find relevant information quickly through keyword search.
Some common use cases include e-commerce product discovery, content management systems,
knowledge bases, and any application where traditional exact match database queries
fall short of delivering intuitive search experiences. To understand why it's useful
in these scenarios, let's look at an example. Imagine we're working on a recipe app and we
want to allow users to search for different foods and recipes. We could use find with regex to query
the database for matching documents. For example, we can look up pasta using this query and we'll
find documents that contain the word pasta, like this one, prosciutto and parmigiana pasta.
However, with this regex query, we have to scan all characters in the name field,
which can be very inefficient with large datasets. Additionally, these types of queries don't support
the advanced search features that users expect from modern applications, like autocomplete or
fuzzy search. But with MongoDB Atlas Search, we can create a search index, which will provide fast,
relevant results, and build search experiences with all of the features that users expect.
Let's take a closer look at how this works. To use Atlas Search, we need to create a search
index on a MongoDB collection and write search queries using the search or search meta aggregation
stages to retrieve and display relevant results. In this example query, we'll be using search.
So now, let's use this Atlas Search index and query to find matches for the word pasta,
just like we did earlier with that regex query, except that Atlas Search is going to make this
search much more efficient and easier to manage. By now, you probably know how indexes and queries
work in MongoDB, but in Atlas Search, they work a little differently. Atlas Search has its own
indexing technology based on Apache Lucene. The Atlas Search process, MongoT, acts as a bridge
between MongoDB's query engine and Lucene's search engine, handling both text indexing and querying.
When creating a search index, the data from the collection is transformed into a structured format
using a process called tokenization, where text fields are broken into smaller components,
like words or phrases. We can customize how data is tokenized with different analyzers.
These tokens are then stored in the index along with relevant metadata for quick retrieval during
search queries. Let's take a closer look at our example to learn more about how this works.
To implement Atlas Search, the first thing we need to do is create a search index. For our recipe app,
let's create a search index for the food collection with the createSearchIndex method
in the MongoDB shell. First, we come up with an index name so that we can use it in our search
queries. Let's use foodNameIndex for the index name. Next, we can decide to index specific fields
or all fields in the document. We'll talk more about this later when we discuss static and
dynamic mappings. Here, we're only indexing the dish field so that users can use keywords to
find dishes. We'll also need to provide the data type of the dish field, which, in this case,
is a string. Once we create this index, Atlas uses the Lucene engine to process and organize
data in the food collection. With the search index set, the MongoT process can query documents
we want to search. So now, how do we execute a search query? We need to use another powerful
tool known as aggregation. If you haven't already or simply need a refresher, check out our aggregation
skill. To use aggregation with search, we use the search stage or search meta stage. Search is used
to return documents that match the search criteria, while search meta is used to retrieve metadata
about the search results. It's important to note that the search stages must be first in the
aggregation pipeline for a query to execute correctly. For example, if we were to place a
match stage before a search stage, we would get an error. This is because the match stage creates a
new data set that no longer has access to the search index. The search stage performs a full-text
search on a specified field or fields that are defined within the search index. Let's look at the
search query from our example earlier. Here, we created an aggregation pipeline that uses the
search stage with the food name index index created earlier. Next, we specify our query criteria.
Here, we're using the text operator to perform a text search. We'll explore more available operators
in future lessons. Finally, we define the query and the field path we want to search on.
We're searching for mentions of pasta within the dish field for now.
When we run this search query, every document that's returned has pasta mentioned in the dish
field. Atlas Search assigns a relevance-based score to each document in the result set.
It then returns these documents ordered from highest to lowest score, making sure we see the
most relevant results first. So, in our example, we know that these two documents at the top of the
return set have higher scores and are most closely associated with the search term pasta. This
relevance scoring uses the BM25 ranking algorithm, which is designed to analyze the relationship
between query terms and index data in your MongoDB collection. MongoDB Atlas Search also has
additional controls for customizing scoring behavior, allowing you to tailor relevance
ranking for specific fields or attributes. Check out our docs to learn more about how to adjust
scoring and ranking. Once we've created our search index and built our search query, there are a few
methods that can help you manage Atlas Search. To view all of your search indexes, you can use the
getSearchIndexes method. To update an existing search index, use the updateSearchIndex method,
along with the new index definition. And to delete a search index, we can use the dropSearchIndex
method. But make sure to evaluate the impact on your application first. You can also easily manage
your search indexes and test out your search queries with MongoDB Atlas and Compass. Ready to
master the fundamentals of Atlas Search? Click the link in the description to claim your skill badge
and boost your expertise. In this skill, we walked you through how to build search indexes and queries
with these considerations in mind, so you can take full advantage of Atlas Search. First, let's recap
what we learned in this lesson. MongoDB Atlas Search is an integrated full-text search service
within Atlas, enabling sophisticated search experiences without separate infrastructure
management. The MongoT process facilitates the ability to create search indexes and search
queries. To use Atlas Search with your Atlas cluster, you must first create a search index
on a collection. You can then perform search queries using the search and search meta aggregation
pipeline stages. Finally, we showed you shell methods that you can use to manage Atlas Search
indexes, including getSearchIndexes, updateSearchIndex, and dropSearchIndex.