在仿照 Ruby-China 的实现写一个搜索的功能,现是添加了 base_model 里的 find_in_batches 才终于能执行rake sunspot:reindex
但是@search = Topic.search
这里总是会提示错误
Mongoid::Errors::InvalidFind in SearchController#index
Calling Document#find with nil is invalid
似乎是跟 Mongoid 的什么冲突了,改成Topic.solr_search
似乎是可以了,
尝试:
r = Topic.solr_search { fulltext 'test' }
results = r.results
又是错误...
NoMethodError: undefined method 'id' for #<Array xxxx>
这里 Ruby-China 是怎么解决的?能否简单解释一下... 先谢谢啦
详细的错误代码:
[parano@u330 attix.us]$ bundle exec rake sunspot:solr:start
:public is no longer used to avoid overloading Module#public, use :public_folder instead
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/resque-1.19.0/lib/resque/server.rb:12:in `<class:Server>'
Removing stale PID file at /home/parano/code/rails_projects/attix.us/solr/pids/development/sunspot-solr-development.pid
Successfully started Solr ...
[parano@u330 attix.us]$ rails console
:public is no longer used to avoid overloading Module#public, use :public_folder instead
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/resque-1.19.0/lib/resque/server.rb:12:in `<class:Server>'
Loading development environment (Rails 3.1.3)
ruby-1.9.3-p0 :001 > p = Post.solr_search { fulltext 'vero' }
=> <Sunspot::Search:{:fq=>["type:Post"], :q=>"vero", :fl=>"* score", :qf=>"title_texts content_texts comments_texts", :defType=>"dismax", :start=>0, :rows=>30}>
ruby-1.9.3-p0 :002 > p.results
NoMethodError: undefined method `id' for #<Array:0xb4916b8>
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/mongoid-2.3.4/lib/mongoid/criteria.rb:382:in `method_missing'
from /home/parano/code/rails_projects/attix.us/vendor/plugins/sunspot_mongoid/lib/sunspot/mongoid.rb:45:in `criteria'
from /home/parano/code/rails_projects/attix.us/vendor/plugins/sunspot_mongoid/lib/sunspot/mongoid.rb:39:in `load_all'
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.0/lib/sunspot/search/abstract_search.rb:228:in `block in populate_hits'
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.0/lib/sunspot/search/abstract_search.rb:224:in `each_pair'
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.0/lib/sunspot/search/abstract_search.rb:224:in `populate_hits'
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.0/lib/sunspot/search/hit.rb:90:in `result'
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.0/lib/sunspot/search/abstract_search.rb:275:in `block in verified_hits'
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.0/lib/sunspot/search/paginated_collection.rb:50:in `select'
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.0/lib/sunspot/search/paginated_collection.rb:50:in `method_missing'
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.0/lib/sunspot/search/abstract_search.rb:275:in `verified_hits'
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.0/lib/sunspot/search/abstract_search.rb:59:in `results'
from (irb):2
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /home/parano/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'ruby-1.9.3-p0 :003 >