I have text index in MongoDB and want to use text command for searching in my collection. Can't find this functionality in Mongoid.
2 Answers
I have tried to find the solution as I am also a mongoid user. I haven't found anything specific related to search text in mongoid other than where query. After little investigation I have found mongoid_search gem which could be useful for search text.
It has one limitation that is written in very first line of readme is "If your searchable model is big (i.e. 1.000.000+ records), solr or sphinx may suit you better." If you have so you need to configure solr or elasticsearch for better performance. If you are going to deploy on heroku then please develop in that consideration too.
May be this would help you to achieve your goal.
Comments
Recently ran into a similar situation. You can run a search command in Mongoid in the following manner:
session = Mongoid.session('default')
session.command({"text" => 'my_collection', 'search' => 'whatever'})
Makes for a nice mixin:
# mixins/search.rb
module Mixins
module Search
extend ActiveSupport::Concern
module ClassMethods
def search(query)
session = Mongoid.session('default')
session.command({"text" => collection.name, 'search' => query})
end
end
end
end
That way you can just include Mixins::Search into your models and use Model.search 'whatever' to search for stuff.