ruby-on-railssolrsunspot-solr

Implement Solr Search into Rails Project


I am trying to implement solr search into one of my rails project. My problem statement is to be able to search upon my models and show relevant results along with auto suggest. Could someone help me to complete it properly. At the moment I am trying to use sunspot solr although its not working for me as expected. I can see some indexing has been created in my project but the search is not working. Also there is no gem for auto suggest. Below are some snippets of my code. And yes I have gone through other links for solr and it did not solve my problem.

Category Model

class Category < ActiveRecord::Base
  attr_accessible :name, :parent_category_id, :image, :image_file_name,     :image_content_type, :image_file_size
  has_and_belongs_to_many :events, :join_table => :categories_events

  has_attached_file :image

  searchable do
    text :name
  end

end

My Controller

if params.has_key?(:category)
  puts "Inside Index Search"
  puts params[:category]
  @search = Category.ransack do 
    fulltext params[:category]
  end
  @category = @search.result.first

I am getting the results into @category and using it in my view to display.

Thanks in advance. I really appreciate your help. :-)


Solution

  • I solved it. There was a conflict between Solr and Ransack gem and that's why I could not get the resultset. It needed the below changes and its working now.

        @search = Sunspot.search(Category) do 
          fulltext params[:search]
        end
        @category = @search.results
    

    EDIT

    My answer might not seem complete so just adding the code snippet that worked for me.

    My Model

    searchable do
        text :name, :as => :name_textp
        text :description, :as =>  :description_textp
        text :category_strings, :as => :category_strings_textp
        string :get_valid_dates, :multiple => true
        integer :category_ids, :multiple => true
        boolean :active
        boolean :company_display
    end
    

    My Controller

    search_results = Sunspot.search(Event) do
        fulltext current_search
        with(:category_ids, [ id ])
        with(:active, true)
        with(:company_display, true)
        paginate(:page => @current_page, :per_page => page_size)
    end
    

    Hope it helps.