Search Fundamentals / Facets

Press O for more options
5:04

Video Transcript (English)

When users perform searches, they often need to understand the scope of their results, not just the matching content itself. They want to know, for example, how many products or movies actually match their query.

By combining facets with the $searchMeta stage in Atlas Search, we can provide users with detailed breakdowns of their search results by categories. This allows us to show not only the total number of matches, but also how those matches are distributed across various attributes like genre, rating, or release date.

In this video, you’ll learn how to create this user experience by defining a search index for facets. Then, you’ll learn how to create a facet using the searchMeta stage to complete the second Feature for our mFlix app.

Finally, we’ll show you a few methods that you can use to manage your Search indexes.

Let’s get started!

We’ve been working on two features to implement Atlas Search in a movie streaming app.

We completed the first feature, and now we need to create Feature 2 which enables users to determine how many movies are in each genre within a date range.

These results will display in a sidebar to make it easier for customers to narrow down their search results.

To do this, we’ll create a new search index using facets and then build a new Search query that uses the $searchMeta stage.

First, let’s dive into facets. Facets give us the ability to group search results by a category or range, and return the count for each specified group. These groups are generally referred to as buckets.

In our case, each of our buckets will be a different movie genre.

To start using facets, let’s define a search index that can be used for facets.

We’ll name this new search index genresFacetedIndex.

Faceted indexes cannot be dynamic because they require a special field type. So we’ll set the dynamic option to false to use static mappings.

Now we specify the fields to index.

We want to bucket the results based on the genres field, so we add genres to the field mapping with a “token” type.

The genre field is a string, but we must use the Atlas Search token type in order to enable faceting on these fields. With the token type, Atlas Search indexes the terms in the string as a single token for efficient filtering or sort operations.

The last field we’ll index is the released field, which we’ll use to search for a range of movie release dates.

With our new search index successfully created, we can now create our search query that finds movies based on their release date and buckets them by genres.

Before we begin, let’s recap what the $searchMeta stage does. The searchMeta stage is used to return metadata about your results.

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

To retrieve counts of each genre, we'll begin by placing the $searchMeta stage at the start of our pipeline.

Next, we define the search index as genresFacetedIndex.

Within the $searchMeta stage, we utilize the facets collector allowing us to retrieve detailed breakdowns and counts of our search results by categories.

Inside the facets collector, for each field you want to categorize, you use a specific facet operator. These operators, like range, define how Atlas Search should collect and group the data for that facet.

Since we want to search for movies in a particular date range, we'll use the range facet operator.

In this case, we're looking for movies with a release date between January 1st, 2000, and January 31st, 2000.

Now that we have our facet collector figured out, we can move on to defining the buckets for each genre.

This will help us see the movie counts for each genre within the specific date ranges we've chosen.

Within the facet collector, we define a subdocument field named facets. Inside this subdocument we can specify multiple facets or groupings; each with its own data type, path and some optional fields.

First, we define the name of the facet. This is how the facet counts will be referenced in the results. We’ll call it genresFacet.

Now we provide a couple of parameters, including the field we are querying and its data type. In this case, we are querying the genres field, which is a string.

When we run our search query, we get a document with the name of our facet and each genre in its own bucket.

Remember that the count includes only movies from January 1, 2000 to January 31, 2000 since we defined the range operator.

Optionally, we can also limit the number of buckets in our search query by using the numBuckets option. Let’s see what happens when we set numBuckets to two.

Now we see only the two buckets with the highest count, the Drama and Comedy genres.

Nice work! Now users will be able to see a count of movies that match their search criteria by genre.

In this video, you learned that facets group results by a category or range, and return the count for each of the specified groups. These groups are generally referred to as buckets.

We finished feature two for our movie streaming app, mFlix by defining the search index needed for facets, and creating a search query with the $searchMeta stage and facets collector.