How to Use Facets with Atlas Search
Atlas Search facet and count are in preview, but can be used in production applications. If there are any syntax or behavior changes between the preview stage and general availability (GA), we will proactively communicate before introducing any breaking changes. The MongoDB Cloud Support team will help troubleshoot any issues related to using this feature as part of your contract.
This tutorial describes how to create an index with a facet
definition on string, date, and numeric fields
in the sample_mflix.movies
collection. It shows how to run an Atlas Search
query against those fields for results grouped by values for the string
field and by ranges for the date and numeric fields, including the
count for each of those groups. It takes you through the following
steps:
- Set up an Atlas Search index with facet definition on the
genres
,released
, andyear
fields in thesample_mflix.movies
collection. - Run Atlas Search query against the
released
field in thesample_mflix.movies
collection for results grouped by values for thegenres
field and by ranges for theyear
field.
Prerequisites
To complete these tutorials, in addition to the prerequisites listed in the Tutorials page, you must have an Atlas cluster running one of the following versions:
- MongoDB 4.4.11 or later
- MongoDB 5.0.4 or later
Create the Atlas Search Index for Facet
In this section, you will create an Atlas Search index on the genres
,
year
, and released
fields in the sample_mflix.movies
collection.
Navigate to the Atlas Search page for your project.
- If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
- If it is not already displayed, select your desired project from the Projects menu in the navigation bar.
- Click your cluster's name.
- Click the Search tab.
Enter the Index Name, and set the Database and Collection.
In the Index Name field, enter
default
.NoteIf you name your index
default
, you don't need to specify anindex
parameter when using the $search pipeline stage. Otherwise, you must specify the index name using theindex
parameter.- In the Database and Collection section, find the
sample_mflix
database, and select themovies
collection.
Specify an index definition.
The following index definition uses lucene.standard
as the
default analyzer for both indexing and querying the fields and
specifies the following for the fields to index:
Field Name | Data Type |
---|---|
genres | |
year | |
released |
Atlas Search support for faceting on numeric and date fields using
number and date type is
being deprecated. Although you can continue using existing
facet definitions on the number
and date
types,
make a note of the following:
- We recommend using the new numberFacet and dateFacet types in all index definitions for faceting on numeric and date fields, respectively.
- Support for faceting on numeric and date fields using number and date types will be removed in August 2022.
You can use the Visual Editor or the JSON Editor in the Atlas user interface to create the index.
Visual Editor
- Click Next.
- Click Refine Your Index.
- Change Dynamic Mapping to Off.
Add the following fields in the Field Mappings section:
Field NameDynamic MappingData Type Configurationgenres
Change Enable Dynamic Mapping to Off.Click Add Data Type, and select StringFacet.year
Change Enable Dynamic Mapping to Off.Click Add Data Type, and select NumberFacet.released
Change Enable Dynamic Mapping to Off.Click Add Data Type, and select Date.
JSON Editor
Replace the default definition with the following:
{ "mappings": { "dynamic": false, "fields": { "genres": { "type": "stringFacet" }, "year": { "type": "numberFacet" }, "released": { "type": "date" } } } } - Click Next.
Search the Collection
➤ Use the Select your language drop-down menu to set the language of the examples in this section.
You can use facet in queries that use the
$search
and $searchMeta
stages. In this
section, connect to your Atlas cluster and the run the sample query
against the sample_mflix.movies
collection using the
$searchMeta
stage. MongoDB recommends using the
$searchMeta
stage to retrieve metadata results only.
The results show metadata results for two types of facet search. The
genresFacet
document shows the number of movies in each genre and
the yearFacet
document shows a count of the number of movies
within the boundaries:
1910
, inclusive lower bound the1910
bucket1920
, exclusive upper bound for the1910
bucket and inclusive lower bound for the1920
bucket1930
, exclusive upper bound for the1920
bucket and inclusive lower bound for the1930
bucket