I’m making a recipe blog website using mongoose, nodejs and express. I am making a search form, but the text search not giving the correct output.
In Recipe.js schema I added the following parameters, that in which values I want to search, I’m added the following ones: recipeSchema.index({ name: 'text', description: 'text', ingredients: 'text', categoryByNationality: 'text', categoryByServing: 'text'});
When I started this project, i had only recipeSchema.index({ name: 'text', description: 'text',});
and it’s worked, but after a few months later I added the ingredients, categoryByNationality and categoryByServing and when I search for category, not finding any recipe. If I search for name and description, its working fine. When I delete all this recipeSchema.index({.......})
code, working with the name and with description again.
What is causing this problem, any idea?
Controller.js
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: 'Cooking Blog - Search', recipe });
} catch (error) {
res.status(500).send({message: error.message || "Error Occured"});
}
}
Recipe schema
const mongoose = require('mongoose');
const recipeSchema = new mongoose.Schema({
name: {
type: String,
required: 'This field is required.'
},
description: {
type: String,
required: 'This field is required.'
},
servings: {
type: Number,
required: 'This field is required.'
},
quantity: {
type: Array,
required: 'This field is required.'
},
ingredients: {
type: Array,
required: 'This field is required.'
},
categoryByServing: {
type: String,
enum: ['Reggeli', 'Ebéd', 'Vacsora', 'Desszert', 'Levesek', 'Egyéb'],
required: 'This field is required.'
},
categoryByNationality: {
type: String,
enum: ['Thai', 'Kínai', 'Indiai', 'Olasz', 'Angol', 'Magyar', 'Egyéb'],
required: 'This field is required.'
},
image: {
type: Array,
required: 'This field is required.'
},
comments: [
{
username: String,
comment: String,
date: {
type: Date,
default: Date.now
},
rating: Number,
},{
timestamps: true
}
],
count: {
type: Number
},
likes: {
type: Number
},
ratingAvg: {
type: Number
},
recipe_id: {
type: String
}
});
recipeSchema.index({ name: 'text', description: 'text', ingredients: 'text', categoryByNationality: 'text', categoryByServing: 'text'});
module.exports = mongoose.model('Recipe', recipeSchema);
search.ejs
<h1 class="pb-4">Search result</h1>
<div class="row row-cols-2 row-cols-lg-5 g-2 g-lg-3">
<% if(typeof recipe !== 'undefined' && recipe.length > 0) { %>
<% recipe.forEach(function(recipe, index){ %>
<a href="/recipe/<%= recipe._id %>" class="col text-center category_link">
<div class="category_img category__img--large shadow">
<img src="/uploads/<%= recipe.image %>" alt="<%= recipe.name %>" loading="lazy">
</div>
<div class="pt-1"><%= recipe.name %></div>
</a>
<% }) %>
<% } else { %>
<p>Cant find any recipe</p>
<% } %>
</div>
Sample data for Recipe database
{
"_id": {
"$oid": "6228ce14b72c51796e00b8ce"
},
"name": "Test recipe",
"description": "test test test",
"servings": {
"$numberInt": "6"
},
"quantity": ["12", "20", "2"],
"ingredients": ["l water", "g salt", "l milk"],
"categoryByServing": "Ebéd",
"categoryByNationality": "Indiai",
"image": ["1646744388278274327802_7801262463232661_4968108751943235625_n.jpg"],
"comments": [{
"username": "First username",
"comment": "First comment",
"rating": {
"$numberInt": "5"
},
"_id": {
"$oid": "622753706aff227ef4b4460a"
},
"date": {
"$date": {
"$numberLong": "1646744433049"
}
}
}, {
"username": "Second username",
"comment": "Second comment",
"rating": {
"$numberInt": "4"
},
"_id": {
"$oid": "6227537e6aff227ef4b44615"
},
"date": {
"$date": {
"$numberLong": "1646744446326"
}
}
}, {
"username": "Third username",
"comment": "Third comment",
"rating": {
"$numberInt": "3"
},
"_id": {
"$oid": "622753896aff227ef4b44622"
},
"date": {
"$date": {
"$numberLong": "1646744458103"
}
}
}, {
"username": "Fourth username",
"comment": "Fourth comment",
"rating": {
"$numberInt": "2"
},
"_id": {
"$oid": "6227539e6aff227ef4b44631"
},
"date": {
"$date": {
"$numberLong": "1646744478781"
}
}
}, {
"username": "Fifth username",
"comment": "Fifth comment",
"rating": {
"$numberInt": "1"
},
"_id": {
"$oid": "622753a86aff227ef4b44642"
},
"date": {
"$date": {
"$numberLong": "1646744489088"
}
}
}, {
"username": "25",
"comment": "gasss",
"rating": {
"$numberInt": "3"
},
"_id": {
"$oid": "622796829f8062416492dcfd"
},
"date": {
"$date": {
"$numberLong": "1646761603076"
}
}
}],
"count": {
"$numberInt": "120"
},
"likes": {
"$numberInt": "0"
},
"ratingAvg": {
"$numberDouble": "3.0"
},
"__v": {
"$numberInt": "6"
}
}