Step1:新建项目
Step2:脚手架
rails generate scaffold Post name:string title:string content:text
Step3:数据库变更
rake db:migrate
Step4: 加入gem包
gem 'sunspot_rails'
gem 'sunspot_solr' # optional pre-packaged Solr distribution for use in development
Step5:
rails generate sunspot_rails:install
Step6:启动solr
bundle exec rake sunspot:solr:start # or sunspot:solr:run to start in foreground
Step7:修改models/post.rb
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title
searchable do
text :content, :name,:title
end
end
Step8: 建立索引
方法1:
rake sunspot:reindex
方法2:
rails c 中
Post.reindex
Step9:search rails c 中
search =Post.search { fulltext 'a' } #search keyword a
search.total #results count
results =search.results # results