【问题标题】:Mongoose - Can't do partial search even with a text indexMongoose - 即使使用文本索引也无法进行部分搜索
【发布时间】:2019-10-12 02:26:09
【问题描述】:

我的快递应用必须进行部分搜索。

我的主题模型:

var mongoose = require("mongoose");

var subjectschema = new mongoose.Schema({
    Subject_Name: String,
    Subject_DOB: Date,
    Subject_Address: String,
    Subject_City: String,
    Subject_Prov: String,
    Subject_zip: String,
    Subject_Country: String
},{ strict: false });

subjectschema.index({ Subject_Name: "text" })

module.exports = mongoose.model("subject", subjectschema);

我的搜索路线(POST):

router.post("/visit", middleware.isLoggedin, function(req,res){
    var text = req.body.user;
    subject.find({ $text:{$search: text}}).sort('Visit_date').exec(function(err,returnedsubjects){
        if(err){
            console.log(err);
        } else {
            console.log(returnedsubjects);
            res.render("visit",{returnedsubjects: returnedsubjects});
        }
    });
});

MongoDB 中的Subject_Name 是“Steven Smith” * 我的搜索不区分大小写(太棒了!)。可以搜索“史蒂文”或“史蒂文”。 * 我可以做部分完整的话。可以搜索“Steven”或“Smith”

但如果我搜索“Steve”或“ste”,我认为它会找到记录。但它没有。

我该如何进行搜索?

谢谢!

【问题讨论】:

    标签: node.js express mongoose


    【解决方案1】:

    Mongo 文本索引不支持部分或模糊搜索。 https://docs.mongodb.com/manual/text-search/

    将其视为更多的关键字搜索。您可以使用不推荐使用的正则表达式,因为它不使用任何索引,并且会对集合执行全扫描。

    ElasticSearch 是一个更优化的字符串搜索数据库。

    或者你可以使用 Algolia https://www.algolia.com/

    【讨论】:

    • 太棒了。非常感谢!
    猜你喜欢
    • 2019-06-03
    • 2013-04-06
    • 1970-01-01
    • 2013-03-31
    • 2016-03-14
    • 2020-09-24
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    相关资源
    最近更新 更多