Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full text search using Mongoid

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.
like image 870
brayne Avatar asked Dec 06 '22 08:12

brayne


1 Answers

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]}\"" })

like image 119
Sean Dixon Avatar answered Dec 15 '22 04:12

Sean Dixon