2016-11-15 112 views
0

我似乎無法找到我錯了什麼。全文搜索的貓鼬索引

如果有人能指點正確的方向,那將是巨大的:

我的架構:

var postSchema = new Schema({ 
    postID:   String, 
    title:   String 
}); 
mongoose.model('post', postSchema, 'posts'); 
postSchema.index({title: 'text'}); 

這是爲什麼不工作:

apiRouter.get('/api/searchPosts', function(req, res, next){ 
    postModel.find(
     { $text : { $search : req.query.text } } 
    ) 
    .exec(function(err, posts) { 
     if(posts){ 
      console.log('The query param is: ' + req.query.text); 
      console.log('The posts results is: ' + JSON.stringify(posts)); 
      res.json({ 
       posts  : posts 
      }); 
     } else { 
      res.send('Post does not exist'); 
     } 
    }); 
}); 

結果我得到是:

查詢參數是:Shayan

的帖子結果是:[]

Post表的標題字段中有結果「砂眼」爲什麼不是這方面的工作?

任何輸入都會有所幫助。一如既往感謝

Shayan

回答

0

明白了。問題是,我需要在shell中運行以下:

db.posts.createIndex({"title":"text"}) 

它完美現在