2015-12-21 2 views
1

Я пытаюсь создать простой текстовый поиск по комбинированному индексу.текстовый поиск mongoose не возвращает результаты

Вот мой мангуст модель:

// 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 
    } 
] 

ответ

1

Вы пробовали переиндексации коллекцию?

команда

Монго:

db.collection.reIndex(); 

Проблема была с тем, как я был индексации. Использование двойных кавычек не работает:

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

Однако одиночные кавычки делает:

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

Я попытался индексирование этого, казалось переиндексации, но поиск не по-прежнему ничего возвращения. – Cookiejest

+0

Что такое вывод «db.items.getIndexes();» – Jared

+0

Я обновил вопрос с выходом db.items.getIndexes. Спасибо за помощь!! – Cookiejest

Смежные вопросы