Lesson 1: Atlas Search Fundamentals / Learn

Video Transcript (English)

Before we begin using Atlas Search for our movie catalog app, let’s discuss how it works behind the scenes.

In this video, we’ll explore the Atlas Search process known as mongot, gaining insights into its role and how Atlas Search indexes and queries work.

By now, you probably know how indexes and queries work in MongoDB, but in Atlas Search they work differently.

Atlas Search has its own indexing technology based on Apache Lucene.

So how do we manage these indexes? That’s where mongot comes into play.

The Atlas Search process, mongot, is an abstraction layer that includes the Apache Lucene engine.

The process interfaces with the rest of Atlas Search to enable a seamless integration with MongoDB.

Using Apache Lucene, the mongot process facilitates search by creating search indexes based on rules we supply in the index definition.

Let's go through an example of a search index.

Here we’re creating a search index for the movies collection by using the createSearchIndex method inside the Mongo Shell. This search index will index the plot field, making it easy for end users to find relevant movie plot descriptions based on keywords.

First, we provide the name of the index. In this case, we’re naming it plot index.

Next, we can define our search indexes by choosing to index specific fields or all fields in the document. You’ll learn more about this in a future lesson when we discuss static and dynamic mappings.

Here, we’re indexing only the plot field.

We also need to define the expected data type. In this case, we know the plot field will be a string. We’ll dive deeper into data types in later lessons.

Similar to other indexes, search indexes take up space on disk and are continuously updated when documents are inserted, updated, or deleted.

The mongot process uses MongoDB Change Stream technology to keep search indexes up to date.

Now that the search index is 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, we invite you to look at our catalog content on aggregation.

In particular, we use the search or searchMeta stage.

It is important to note that the search stages must be the first stage 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 dataset 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 in the search index.

Let’s add on to our earlier example.

Here we created an aggregation pipeline that uses the search stage with the plot index created earlier.

Next, we define what we want to query for. In this case, we’re using the text operator to perform a text search. We’ll get more acquainted with the available operators in future lessons.

Finally, we define the query and the field path we want to search on. Here we’re searching for mentions of space within the plot field.

When we run this, every document that’s returned has space mentioned in the plot field.

We can make the results easier to read by adding a limit and project stage to return only the title and plot fields. We can also use the meta aggregation operator in the projection stage, which allows us to see how the results were scored.

Now we can easily see the title and plot fields in our results. We also see how each document was scored. Atlas Search has a relevance based scoring system. The document with the highest score is the most relevant and is returned first.

Atlas Search gives documents a higher score if the query term appears frequently in a document, and a lower score if the query term appears across many documents in the collection.

Earlier, we mentioned that we can also use the searchMeta stage.

This stage is used to return a summary of your results.

For instance, if you wanted to obtain the number of documents from a search query, it would be more performant to use the searchMeta stage instead of adding a count stage to the end of your search pipeline.

Like the search stage, the searchMeta stage must be used at the beginning of the aggregation pipeline.

Let’s update our previous search query to use the searchMeta stage and add the count field to it

We should see that it counts the total number of documents returned from the query. In this case, there are 97 documents returned.

Awesome job!

In this video, you learned that the mongot process facilitates the ability to create search indexes and search queries.

Next, you learned that Atlas Search indexes are required to perform a search query.

Finally, we used the search and searchMeta aggregation pipeline stages to perform search queries.