Search code examples
ruby-on-railssolrenumssunspot

rails 4 sunspot with enum where conditional


I have configured my model to be searchable by sunspot:

enum status: [ :draft, :unreviewed, :reviewed, :publish, :disable, :reject ]

searchable do
    text :name, :boost => 5
    text :description
    integer :status
    time :created_at
end

and in the controller:

@search = Product.search do
  fulltext params[:search]
  with :status, 3
  order_by :created_at, :desc
  paginate :page => params[:page], :per_page => 10
end

this line with :status, 3 used to be filter products with status publish which is I set using rails enum. But I got no results.

What did I miss? thanks


Solution

  • Just a small change in your your controller

    @search = Product.search do
      fulltext params[:search]
      with(:status).equal_to(3)
      order_by :created_at, :desc
      paginate :page => params[:page], :per_page => 10
    end
    

    and

    rake sunspot:reindex

    to reindex your data

    These are different type you can use with

    with(:blog_id, 1)
    with(:blog_id).equal_to(1)
    
    with(:average_rating, 3.0..5.0)
    with(:average_rating).between(3.0..5.0)
    
    with(:category_ids, [1, 3, 5])
    with(:category_ids).any_of([1, 3, 5])