This repository contains sample code to implement 'nested facets' in MongoDB Atlas Search.
Video demo: https://youtu.be/EUmCLfMse8Q
Simply put they are hierarchical filters that allow you to navigate structured relationships in your search queries. Here's a picture using the sample_airbnb.listingsAndReviews
sample data that comes with your MongoDB Atlas installation.
This implementation of nested facets relies on having a specific data structure that allows faceting on child values and maintaing those values to their parents. Here is an example of a document with values from the example above:
{
_id:"7660936",
details:{
level0:"United States"
level1:"United States/Oahu"
level2:"United States/Oahu/House"
level3:"United States/Oahu/House/Private room"
level4:"United States/Oahu/House/Private room/Real Bed"
},
...
}
Then this field is indexed in Atlas Search as:
"details": {
"dynamic": false,
"fields": {
"level0": [
{
"type": "stringFacet"
},
{
"type": "token"
}
],
"level1": [
{
"type": "stringFacet"
},
{
"type": "token"
}
],
"level2": [
{
"type": "stringFacet"
},
{
"type": "token"
}
],
"level3": [
{
"type": "stringFacet"
},
{
"type": "token"
}
],
"level4": [
{
"type": "stringFacet"
},
{
"type": "token"
}
]
},
"type": "document"
},
For full example outputs and aggregation pipeline see this example
This app allows you to use any existing database and collection to automatically create the appropriate structure. The app also writes the aggregation pipeline that you need based on the levels of nesting you want.
The app also provides front end components that allow you to render the facets and then use them to filter your search query.
You will need:
- Access to a MongoDB Atlas cluster
- NodeJS installed
Set your environment variables. Simply copy the example.env file and rename it to .env
and then make your changes.
You may also need to change your schema values in the frontend to make the search results render correctly.
Run
npm install
Run the setup.mjs script
node setup.mjs
This script will create the write index definition and add it your cluster. It also creates the write data model by running a $merge
aggregation pipeline on your collection.
To run in developer mode simply execute
npm run dev
You will then be able to access the frontend at http://localhost:3000