Giter Site home page Giter Site logo

atlas-search-nested-facets's Introduction

What is this?

This repository contains sample code to implement 'nested facets' in MongoDB Atlas Search.

Video demo: https://youtu.be/EUmCLfMse8Q

What are 'nested facets'?

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.

nested_facets

How do nested facets work?

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

How does this app work?

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.

Running the app

You will need:

  • Access to a MongoDB Atlas cluster
  • NodeJS installed

Setup

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.

Run the frontend

To run in developer mode simply execute

npm run dev

You will then be able to access the frontend at http://localhost:3000

atlas-search-nested-facets's People

Contributors

johngunderwood avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.