Lesson 6: Creating Search Facets / Learn

Video Transcript (English)

Now, in our movie catalog, we want to be able to search for all the available genres and determine how many movies are in each genre.

In this video, you’ll learn how to define a search index for facets. Then, you’ll learn how to create a facet using the searchMeta stage. Let’s get started!

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

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

Now, 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

For example, have you ever searched for a product on a website and in the sidebar you see a list of brands with an associated number next to them? This is the result of a faceted search.

This makes it easier for customers to narrow down their search results. We can do something similar with our movie catalog. Instead of brands, we’ll use movie genres.

Now, to start using facets, let’s create a search query to figure out how many movies within a release date range are in each genre. To do this, we need to combine what we learned about the range operator with facets.

Before we can create our search query, we need to 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 must set the dynamic option to false.

Now we specify the fields to index.

Next, we want to bucket the results based on the genres field.

Let’s add genres to the field mapping with a “stringFacet” type since genres are a string.

We are not limited to creating facets only from strings with the stringFacet field type. We can also use dateFacet for dates and numberFacet for numeric values.

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.

First, we use the searchMeta stage because we’re retrieving the count of each genre.

Then we define the search index as genresFacetedIndex.

Next, we define a facet operator. Here, we can use any operator available in Atlas Search.

Because we want to search for movies in a particular date range, we use the range 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! 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.

You also learned how to create and define the search index needed for facets, and the parameters to provide when using the $searchMeta stage.