使用Mongoid进行全文搜索

bra*_*yne 4 ruby mongodb mongoid

有没有办法通过Mongoid使用MongoDB(v 2.4)的全文搜索功能?我尝试了谷歌组链接的答案,但一直收到以下错误.

在一个选项卡中,我启动了mongod:~$ mongod --setParameter textSearchEnabled=true 导致错误的行: Article.mongo_session.command({:text => {:search => 'Ruby'}})

如果有人能指出runCommand在Ruby中执行MongoDB的方法会很棒,这样我就可以直接运行命令了db.collection.runCommand( "text", { search: <string> })

failed with error 13111: "exception: wrong type for field (text) 3 != 2"

See https://github.com/mongodb/mongo/blob/master/docs/errors.md
for details about this error.
Run Code Online (Sandbox Code Playgroud)

Sea*_*xon 12

对于任何发现这一点的人,Mongoid 4.0允许这样的全文搜索:

Model.text_search('query')
Run Code Online (Sandbox Code Playgroud)

您可以像往常一样进行链接:

Listings.in(category: sections).where(listing_type: 'Website').text_search(params[:q]).to_a 
Run Code Online (Sandbox Code Playgroud)

索引也很简单:

index({name: "text", description: "text"}, {weights: {name: 10, description: 2}, name: "TextIndex"})
Run Code Online (Sandbox Code Playgroud)

编辑:2017年7月

如下所述,text_search已弃用.我一直在使用where()这样:

Model.where('$ text'=> {'$ search'=>"\"#{params [:q]} \""})