Search Fundamentals / Facets
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 search meta 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 search meta stage to complete the second feature for our mFlix app. 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 two, which enables users to determine how many movies are within 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 search meta 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 genres faceted index. 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 genre. Before we begin, let's recap what the search meta stage does.
The search meta stage is used to return metadata about your results. Like the search stage, the search meta stage must be used at the beginning of the aggregation pipeline.
To retrieve counts of each genre, we'll begin by placing the search meta stage at the start of our pipeline. Next, we define the search index as genres faceted index. Within the search meta 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'll be looking for movies with a release date between January first, two thousand and January thirty first, two thousand. 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 specified date ranges that we've selected. Within the facet collector, we define a sub document field named facets. Inside this sub document, 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 genres facet. Now we provide a couple of parameters, including the field we're querying and its data type. In this case, we're 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 first, two thousand to January thirty first, two thousand, since we defined the range operator. Optionally, we can also limit the number of buckets in our search query by using the num buckets option. Let's see what happens when we set num buckets 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 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 search metastage and facets collector.
