Improving MongoDB Text Search Performance

回眸只為那壹抹淺笑 提交于 2021-02-08 07:49:59

问题


I'm looking for some advice about how improve Text Search performance with MongoDB 2.6. I am also using Mongoose.

My mongoose schema has 'body: String' which contains a large chunk of XML. I ran ensureIndex which took a few minutes...

db.models.ensureIndex({ body:"text"})

I want to search this text with a user defined string.

Model.find({ $text: { $search: searchstr }},function(err,data){ });

While there are only a few thousand documents, the search will often time out ( 2 minutes+). How can I improve my performance? Thanks!


回答1:


AFAIK in general it is recomended to use the pipeline framework insted of the standard find when dealing with textSearch.

For instance doing something as:

db.model.aggregate(
   [
     { $match: { $text: { $search: "text" } } },
     { $sort: { score: { $meta: "textScore" } } },
     { $limit: 10 }
   ]
)

Will return just the first 10 elements with much the most.



来源:https://stackoverflow.com/questions/24514894/improving-mongodb-text-search-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!