I have been working on rails project, which had been made using noSql(Mongoid). Every thing is running fine.The issue is I want to add.. full text search here also. I had been using this gems for this…
gem 'mongoid_fulltext'
and my model file looks like this..
class Keyword
include Mongoid::Document
include Mongoid::FullTextSearch
field :name, type:String
#index :name, unique: true
embeds_many :posts
validates_presence_of :name
validates_uniqueness_of :name
fulltext_search_in :name, :index_name => 'name_index'
end
and in controller.
@keywords = Keyword.fulltext_search(params[:search], :index => 'name_index')
and then @keywords always returns an empty array always.
Thanks
Awieet
Apart from what I’m assuming are formatting errors, the only mistake I can find is that you seem to be naming the index manually.
Maybe in your fulltext_search call you should use
:index_name => 'name_index'instead of:index => 'name_index'.I’d advise just not messing with the default name of the index though, and removing that argument entirely from the method call.
Also, were the records persisted before you added the
mongoid_fulltextgem? If so you’ll need to call theupdate_ngram_indexmethod on the Class object (or each instance) to add them to the index.Other than that, have you checked out the
mongoid_searchgem as an alternative tomongoid_fulltext?https://github.com/mauriciozaffari/mongoid_search
I’ve tried both and find find that this one has a much cleaner implementation and interface. Then again, I only use fulltext search sparingly. You may be using fulltext search more than I, and I’m not sure what the differences are feature-wise, but worth a look.
Hope that helps.