My Question:
How to implement Both Features: Full text-search
and Faceting
Together 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",
}]