2016-11-15 129 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"}) 

它完美现在