MongoDB full Text Search with $match - Javascript

My Question:

How to implement Both Features: Full text-search and FacetingTogether using $match ?

Url I’m trying: (After using both features simultaneously)


"http://localhost:4000/search?value=Samsung%5Nfacet%5Nram=4GB"
"http://localhost:4000/search?value=Samsung%5Nfacet%5Nram=2GB&intstorage=128GB" 
Data Structure. =>`(In Mongodb)`
```Javascript
[
    "title": "Samsung Galaxy A30s",
    "price": "12000",
    "url": "http://example.com",
    "ram": "2GB",             
    "intstorage": "256GB",    
  },
  {
    "title": "Samsung Galaxy M30s",
    "price": "12500",
    "url": "http://example.com",
    "ram": "8GB",           
    "intstorage": "128GB", 
  }
]

For making Search:

app.get('/search', async(request, response) => {
    try {
        let result = await collection.aggregate([
            {
            '$search': {
                'index': 'default',
                'text': {
                    'query': `${request.query.value}`,
                    'path': 'title'
                }
            }
        }, {
            '$limit': 7
        }
        ]).toArray();

        response.send(result)

    } catch (e) {
        console.error(e);
    }
})

Now, For Filtering Results, I’m using $match to filter the Documents

DEMO:

        {
            '$search': {
                'index': 'default',
                'text': {
                    'query': `${request.query.value}`,
                    'path': 'title'
                }
            }
        },
        {
             '$match': {         //$match used once here
              'ram': '4GB'
            }
        },
        {
            '$limit': 7
        }

Output

  [{
    "title": "Samsung Galaxy M30s",
    "price": "12500",
    "url": "http://example.com",
    "ram": "4GB",  
    "intstorage": "256GB",
  },
  {
    "title": "Samsung Galaxy A30s",
    "price": "12500",
    "url": "http://example.com",
    "ram": "4GB",            
    "intstorage": "256GB",
  }]

Hi @Divyanshu_Sah ,

Since the pipeline is an array of stages why can’t you push another match into the query when a facet is inserted?

Please note that there is a compound operator available in search so you can potentially add stages to the search by compounding the search…

Best regards,
Pavel