'mongoose Schema.index() giving wrong output in search nodejs
I'm making a recipe blog, but in the search bar giving wrong output.
I have a categoryByServing called "Leves", so if I type in searchbox "leves", it will show all the recipes which is saved in "Leves" categoryByServing. I find out a wrong output, that in one recipe there was an ingredient called "leve", so if I searched for "leves" I also got this recipe, which is not part for "Leves" categoryByServing and also got the "Leves" category recipes too.
Here is the code for Schema.index():
recipeSchema.index({name: 'text', ingredients: 'text', categoryByServing: 'text', categoryByNationality: 'text'});
Here is the code for searching:
exports.searchRecipe = async(req, res) => {
//searchTerm
try {
let searchTerm = req.body.searchTerm;
let recipe = await Recipe.find({ $text: { $search: searchTerm, $diacriticSensitive: true} });
res.render('search', { title: 'Gasztroblog - Keresés', recipe });
} catch (error) {
res.status(500).send({message: error.message || "Error Occured"});
}
}
Recipe.collection.dropIndexes();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
