Mongoose文本搜索没有返回正确的结果



我正在用猫鼬、nodejs和express制作一个食谱博客网站。我正在制作一个搜索表单,但文本搜索没有给出正确的输出。

Recipe.js模式中,我添加了以下参数,即我要搜索的值,我添加的参数如下:recipeSchema.index({ name: 'text', description: 'text', ingredients: 'text', categoryByNationality: 'text', categoryByServing: 'text'});

当我开始这个项目时,我只有recipeSchema.index({ name: 'text', description: 'text',});,它很有效,但几个月后,我添加了成分,categoryByNationality和categoryByServing,当我搜索类别时,没有找到任何食谱。如果我搜索名称和描述,它可以正常工作。当我删除所有这些recipeSchema.index({.......})代码时,再次使用名称和描述。

是什么导致了这个问题,知道吗?

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"});
}
}

配方模式

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);

搜索.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>

配方数据库的样本数据

{
"_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"
}
}

To"改变";MongoDB中的一个索引,首先必须删除上一个索引。

Recreating an Existing Index
If you call db.collection.createIndex() for an index
that already exists, MongoDB does not recreate the index.

MongoDB手册:db.collection.createIndex((.

最新更新