【问题标题】:mongoose text search not returning results猫鼬文本搜索不返回结果
【发布时间】:2016-03-27 18:53:21
【问题描述】:

我正在尝试在组合索引上创建一个简单的文本搜索。

这是我的猫鼬模型:

// models/user.js
// load the things we need
var mongoose = require('mongoose');



// define the schema for our user model
var itemSchema = mongoose.Schema({
    globalinfo       : {
        ownerobjectid       : String,
        name                : String,
        desc                : String,      
        startdate           : Date,
        enddate             : Date,
        price               : Number,
        status              : String,
        statuscss           : String,
        dateadded           : Date,
        locationline1       : String,
        locationline2       : String,
        locationline3       : String,
        locationtown        : String,
        locationpostcode    : String,
        locationlatitude    : Number,
        locationlongitude   : Number,
        termsapprove        : Boolean,
        friendlyurl         : String,
        itemsearchinfo      : String,
    }
});


itemSchema.index(
                    {
                        "globalinfo.itemsearchinfo": "text", 
                        "globalinfo.name": "text" 
                    }
                ); // schema level

// create the model for users and expose it to our app
module.exports = mongoose.model('Item', itemSchema);

这是我的搜索查询:

Item.find(
        { $text : { $search : "item" } }
    ).exec(function(err, items) {

问题是查询总是不返回结果!

我在模型中有一个文档:

{
    "_id" : ObjectId("56781cb97ae92ff08b55d4f1"),
    "globalinfo" : {
        "friendlyurl" : "item-a",
        "dateadded" : ISODate("2015-12-21T15:37:29.591Z"),
        "itemsearchinfo" : "Woop lawn mower for rent!\nYou should use this space to describe the item in detail and make it appealing\nTo the renter write your stuff here.",
        "statuscss" : "na",
        "status" : "Not Available Yet",
        "locationlongitude" : null,
        "locationlatitude" : null,
        "locationpostcode" : "test",
        "locationtown" : "test",
        "locationline3" : "",
        "locationline2" : "",
        "locationline1" : "test",
        "termsapprove" : true,
        "price" : 3,
        "enddate" : ISODate("2015-12-31T00:00:00.000Z"),
        "startdate" : ISODate("2015-12-23T00:00:00.000Z"),
        "desc" : "\n                                    <h3>woop Lawn Mower for Rent! </h3>\n                                    <p>You should use this space to describe the item in detail and make it appealing to the renter <strong>Write your stuff here.</strong> \n                                </p>",
        "name" : "item A",
        "ownerobjectid" : "56781909155232b7871edb17"
    },
    "__v" : 0
}

db.items.getIndexes() 的输出:

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "whatplot_local_db.items"
    },
    {
        "v" : 1,
        "key" : {
            "_fts" : "text",
            "_ftsx" : 1
        },
        "name" : "itemsearchinfo_text_name_text",
        "ns" : "whatplot_local_db.items",
        "background" : true,
        "weights" : {
            "itemsearchinfo" : 1,
            "name" : 1
        },
        "default_language" : "english",
        "language_override" : "language",
        "textIndexVersion" : 2
    }
]

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    您是否尝试过重新索引该集合?

    Mongo 命令:

    db.collection.reIndex();
    

    问题在于我的索引方式。使用双引号不起作用:

    itemSchema.index(
                        {
                            "globalinfo.itemsearchinfo": "text", 
                            "globalinfo.name": "text" 
                        }
                    ); // schema level
    

    但是单引号可以:

    itemSchema.index(
                        {
                            'globalinfo.itemsearchinfo': "text", 
                            'globalinfo.name': "text" 
                        }
                    ); // schema level
    

    【讨论】:

    • 我尝试重新索引它似乎重新索引,但搜索仍然没有返回任何内容。
    • “db.items.getIndexes();”的输出是什么
    • 我已经用 db.items.getIndexes 的输出更新了这个问题。谢谢你的帮助!!
    • 如果您搜索“item A”而不是“item”,您会得到结果吗?
    • 作为测试,您可以将名称键值移动或复制到文档的根目录吗?重新索引集合并再次搜索?我想知道它是否与嵌套有关。您是否需要将关键参考放在这样的引号中?即:“globalinfo.itemsearchinfo”:“text”,“globalinfo.name”:“text”
    猜你喜欢
    • 2020-03-16
    • 2014-05-30
    • 1970-01-01
    • 2015-08-26
    • 2018-08-22
    • 2023-04-11
    • 2017-12-03
    • 1970-01-01
    相关资源
    最近更新 更多