I am using the atlas search autocomplete feature for a collection of Songs in MongoDb.
If I query the song “Santa Claus Is Comin’ To Town” using autocomplete, I get back many versions of this song by many different artists.
A Song
in my case has a "isPopular": "true"
attribute, if the song is a popular version of the song.
I am looking to prioritize documents that have "isPopular": "true"
so that they are returned first in the autocomplete result list. The way I currently have this search configured does not pay attention to this isPopular
attribute. How might I boost these popular records?
Here is the index that I defined in Atlas Search:
{
"mappings": {
"dynamic": false,
"fields": {
"name": [
{
"foldDiacritics": false,
"maxGrams": 7,
"minGrams": 3,
"tokenization": "edgeGram",
"type": "autocomplete"
}
]
}
}
}
And here is the query I am using to fetch the results using a compound
should
query factoring the isPopular
.
let results = await Song.aggregate([
{
$search: {
index: "song_autocomplete",
compound: {
must: [{
autocomplete: {
query: req.query.name,
path: "name",
tokenOrder: "sequential",
}
}],
should: [{
text: {
query: "true",
path: "isPopular"
}
}],
}
},
},
{
$project: {
name: 1,
_id: 1,
},
},
{
$limit: 10,
},
]);
Would appreciate any workaround here so that results that have isPopular
show up as the first results.