Lesson 2: Managing Atlas Search Indexes via the Atlas CLI / Learn

Video Transcript (English)

The Atlas UI is excellent, but let’s face it: Some of us prefer the CLI.

In this video, you’ll learn how to manage your search indexes by using the Atlas CLI. We’ll show you how to list, create, update, and delete search indexes, using our movie catalog data.

To use the Atlas CLI to manage search indexes, your Atlas account must have one of the following roles: Project Data Access Manager, Project Owner, or Organization Owner.

Let’s start by creating a new search index.

To do this we need a JSON configuration file that lists: The name of the index, the cluster that contains the database and collection that we want to index, the name of the collection and database, and any other configurations we want.

Notice we have set dynamic to true.

Next, let’s use the “atlas clusters search indexes create” command.

We need to provide the name of the cluster that we want to create the search index on. In this example, the cluster is named Cluster0.

We also need to give it the path to our configuration file.

Finally, we’ll need the project ID for our cluster. You can skip this step if your project ID is defined in the atlas config or as an environment variable.

When we run this command in our terminal, it returns a success message indicating the index was created.

Remember we set dynamic to true in the configuration?

This causes the entire document to be indexed.

What if we only want to index a certain field like genres?

Thankfully, we can also update existing search indexes in the Atlas CLI.

To do this, let’s first update the search index configuration.

We start by specifying the field we want to index, along with its data type.

We’ll need the ID for the search index we want to update.

To retrieve this ID, we use the “atlas clusters search indexes list” command along with some options.

First, we provide the projectId. If your project ID is defined in the atlas config or as an environmental variable, you can skip this step.

Next, we specify the name of the cluster we’re using.

Finally, we specify the collection and database that our search index is on.

When we run this in the terminal, we get a list of search indexes.

We need the ID field from the genres index that we created earlier in this video.

Let’s copy that ID to use in our update command.

We’ll use the “atlas clusters search indexes update” command.

First, we provide the ID we just copied.

This is followed by the cluster name, the path to the configuration file, and the project ID.

When we run this command, it returns a success message indicating the index was updated.

Now that we have updated the genres index, we can confirm our updates have been applied.

For this, we use the “atlas clusters search indexes describe” command along with some options.

We start with the ID.

We’ll also need the project ID. Again, if your project ID is defined in the atlas config or as an environmental variable, you can skip this step.

Next, we specify the name of the cluster.

Finally, we can specify the output format. For this example, we’ll use JSON.

When we run this command, we get a JSON object with information about the specified search index.

We can see in the mappings field that the genres search index was correctly updated to index only the genres field.

We also see the current status of our index. In this case, the index is ready to be used.

If we want to delete this search index, we can do it easily with the Atlas CLI.

We’ll use the “atlas clusters search indexes delete” command.

We need the search index ID, as well as the cluster name and project ID.

When we run the command, we’ll get a prompt asking if we want to delete the index.

We type “y” for yes and get a message indicating that the index was deleted.

Great work! In this video, you learned how to list, create, update, and delete search indexes by using the Atlas CLI.

Until next time. Keep learning!