前引:sunspot 类似thiking_sphinx的一个搜索的gem ,它是基于Solr搜索! (它支持的rails 版本是3.0以上)
我做的一个测试项目步骤和注意事项!
1、添加它的gem
gem 'sunspot_rails'
gem 'sunspot_solr'
如果bundle install 报错误,就这样写
gem 'sunspot_rails', :git => 'https://github.com/sunspot/sunspot.git', :ref => '79175ea'
gem 'sunspot_solr',:git => 'https://github.com/sunspot/sunspot', :ref => '79175ea'
用mongo数据库,引入 gem 'sunspot_mongoid'(当然链接mongo库的gem 也要引入)
2,添加你的 model
把需要搜索的字段这样来定义: searchable do
text :name
end
这个地方貌似可以声明自动生成索引,searchable :auto_index => true, :auto_remove => true do
text :name
end
3,生成索引,启动 sunspot 建议你先用sunspot-solr start这个命令看看,有没有报错!如果有报错那就把这个gem加入gem 'escape','0.0.4'
bundle exec rake sunspot:solr:reindex
bundle exec sunspot-solr start -p 8983
4、应该可以搜索了,mongo的不能用search函数 要用TestMongo.solr_search { fulltext 'h' } ,这个问题我也没搞明白!希望高手call me!thanks
当然你的系统要安装有jdk 噢 而且版本是 1.5以上! https://github.com/sunspot/sunspot 这个是gem 的位置