1

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

Juan Mellado
  • 14,973
  • 5
  • 47
  • 54

1 Answers1

0

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_fulltext gem? If so you'll need to call the update_ngram_index method on the Class object (or each instance) to add them to the index.

Other than that, have you checked out the mongoid_search gem as an alternative to mongoid_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.

Vickash
  • 1,076
  • 8
  • 8