Video Transcript (English)
So far, you’ve learned how to manage search indexes by using the Atlas UI and Atlas CLI. Another way to manage your search indexes is via the MongoDB Shell.
In this video, you’ll learn how to list, view, create, update, and delete search indexes by using the MongoDB Shell.
We’ll be using our movie catalog again to work through some examples.
But first, you’ll need the following database privileges: listSearchIndexes, createSearchIndexes, updateSearchIndexes, and dropSearchIndexes.
An easy way to ensure that your database user has these privileges is by using the built-in readWrite role provided by Atlas.
Once you have the correct privileges and roles assigned to a database user, you can begin managing your search indexes.
To start, let’s create a simple search index on the title field in the movies collection.
First, connect to the sample_mflix database by using mongosh.
Once connected, we use the createSearchIndex method on the movies collection.
Next, we provide the name of the index. If we don’t provide a name, the index will be named “default”. In this case, we name it titleIndex to correspond with the field we’re indexing.
After that, we provide a document that defines the configuration of the search index.
For this example, we want to index the title field, which has string type values.
We can confirm the search index was created by using the getSearchIndexes method on the movies collection and passing in the name of our index.
This returns information about the specified search index, such as the: id, name, Status, definition, and its status on each node.
If we want to list all of our search indexes in a collection, we can use getSearchIndexes without an argument.
Let’s say we want to update our title index to have a dynamic mapping so that all fields are indexed instead of just the title field.
We can do this by using the updateSearchIndex method on the movies collection.
First, we provide the name of the index that we want to update.
Next, we define the changes that we want to implement for the search index. In this case, we’re changing it to a dynamic mapping so that all fields are indexed.
We can confirm our index was updated by using the getSearchIndexes method for the title index.
We can see from the output that the status field indicates the update is currently in progress.
Keep in mind that while your search index is updating, the old one can still be used. But once the updated index is built, the old one is no longer usable.
What if we no longer need a particular search index? Like we did with the Atlas UI and CLI, we can delete search indexes by using the MongoDB Shell.
We simply use the dropSearchIndex method on the movies collection and pass in the name of the index we want to delete, which in this case is titleIndex.
When we run the command, we won’t get any feedback. However, we can confirm the change by using the getSearchIndexes method.
Great job! In this video, you learned how to manage search indexes in the MongoDB Shell by using the: createSearchIndex, getSearchIndex, updateSearchIndex, and dropSearchIndex methods.
We hope you’ll join us again for more Atlas Search fun!