Search Fundamentals / Creating an Atlas Search Index: Mappings

5:01
We know that Atlas search indexes are the magic ingredient that transform your MongoDB data into instantly searchable ranked results. So when we're implementing Atlas search, we must build the search index that will be used by our search queries. In this video, we'll continue to build an example application for a movie streaming platform called mFlix. Specifically, we'll begin to build the search index for feature one. During this build process, you'll learn the difference between dynamic and static mappings. You'll also learn how and when to use the appropriate mappings for your search indexes. Let's quickly review what we know about the first movie search feature that we're working on. This feature will allow users to search the movies database for movies matching keywords in the title and plot description. They'll also be able to specify a date range for the movie release. Remember, we already determined that we'll need to index the title, plot, and released fields for this feature. So now, let's take that information and start to actually build the index. First, we'll need to create a name for our search index. Let's use the create search index method in the MongoDB shell and name our index title plot released index. Note that when we use this method, there's an optional type field that allows us to choose between search or vector search. This option defaults to search, so we don't need to include it in this index definition. Next, we'll begin to define our search index. While there are several optional settings we won't use in this example, it's worth noting an important one, defining analyzers. Analyzers are tools that break down your data and search terms into individual words or tokens. How your data is tokenized directly impacts your search results, so this is a crucial setting. In our case, we'll simply use the default standard analyzer, which means we don't need to specifically include it in our index definition. Now, let's focus on the dynamic option under mappings. When creating a search index, we need to define which fields to index. This is known as a field mapping, and we have two options, dynamic or static. First, let's take a look at dynamic mappings. When we create an index with dynamic mappings, we index every field in the document that has a supported data type. More on supported data types later. Dynamic mappings are great for datasets that have unknown or frequently changing fields, like social media or IoT sensor datasets. They're also useful when experimenting with Atlas Search, so you don't have to constantly update your search index depending on the query. However, this flexibility and power come at a cost. Dynamic mappings have increased size and performance impacts compared to static mappings. To see this in action, let's apply dynamic mappings to our mFlex app by setting the dynamic option to true. Now, each field in the movie collection document will be indexed. This means that we can create a search query for any of these fields. Remember mongo t? It keeps a watchful eye on any changes. So even if we were to add a new field for movie producers, it would be indexed automatically. In addition to dynamic mappings, there are static mappings. Static mappings index specific fields instead of the entire document. We can reduce the size of our search indexes by using static mappings because they usually take up significantly less storage than a dynamically mapped index. Moreover, because static mappings are focused on a known document structure, they can avoid the computational overhead of scanning the entire document when changes are detected. Therefore, static mappings can improve performance when the index needs to be updated. Static mappings are ideal if we know the exact shape of our documents and don't anticipate the fields changing. Let's return to the movie document. We know that we'll just be using the title, plot, and release fields for our keyword movie search feature. Instead of creating a dynamic mapping to index the entire document, we'll create a static mapping on just those three fields by setting dynamic to false. Since we're now using a static mapping, we can only create search queries on the title, plot, and released fields when using this index. While it's outside of the scope of our demo, it's important to know that you can use both dynamic and static mappings in a single index. To learn how, check out MongoDB's documentation. Great job. We'll fill in the data types for each field and finish creating our index in the next lesson. Let's quickly recap what we covered in this video. We began to build the search index for feature one of the mFlix movie streaming app. You learned that dynamic mappings index all fields in a document as long as the fields contain a supported data type. Use dynamic mappings if you don't know the shape of your documents or you expect them to change. Importantly, dynamic mappings can impact the performance of your app. You also learned that static mappings index specific fields instead of the entire document. Use static mappings if you know the exact shape of your documents and don't expect the fields to change.