Is there a way to use MongoDB (v 2.4)'s full text search feature via Mongoid? I tried the answer from google group link but kept on getting the following error.
In one tab, I started mongod as such: ~$ mongod --setParameter textSearchEnabled=true
The line that caused the error:
Article.mongo_session.command({:text => {:search => 'Ruby'}})
It would be great if someone could point out a way to execute MongoDB runCommand
within Ruby, so that I could directly run the command 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.
For anyone finding this, Mongoid 4.0 allows for full text search like this:
Model.text_search('query')
You can chain as you normally would:
Listings.in(category: sections).where(listing_type: 'Website').text_search(params[:q]).to_a
Indexes are pretty easy as well:
index({name: "text", description: "text"}, {weights: {name: 10, description: 2}, name: "TextIndex"})
EDIT: July 2017
As reported below, text_search is deprecated. I've been using where() like this:
Model.where('$text' => { '$search' => "\"#{params[:q]}\"" })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With