Search code examples
ruby-on-railsruby-on-rails-4sunspotsunspot-railssunspot-solr

Sunspot not working


> Entity.where(:price => 40000).count
1

> e = Entity.search do
>     with(:price, 0..40001)
> end
> e.results.count
0

Model entity.rb:

class Entity < ActiveRecord::Base
  searchable do
    integer :variant_id
    integer :kind_id
    integer :locality_id
    integer :price
  end
end

Why Sunspot.search and Where return different count?


Solution

  • When you can find data using .where but can't get any hits in your search, you probably just forgot to reindex your data.

    According to the Quick start for the Sunspot Solr client for Ruby:

    Whenever you make changes to how your models are indexed, you will need to rebuild your index for any existing data in your database. To do so, run Sunspot's "reindex" rake task:

    rake sunspot:reindex