Gem 请教一个 Sunspot 配合 Mongoid 出现问题

paranoyang · 2011年12月28日 · 最后由 billie66 回复于 2013年03月08日 · 4007 次阅读

在仿照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 > 

共收到 12 条回复

看不出来你得问题啊,不知道你还装了些什么别的东西

#1楼 @huacnlee 没装什么特别的阿,基本是ruby-china的子集, 没有装mongoid id自增那个gem, Ruby-China里面Topic.search那里没有做特别的处理吗?

#2楼 @paranoyang 我是装上去就能用了,没遇到困难。 应该和自增ID没有关系,sunspot 都有 Mongoid 的 Gem,而那个 Gem 也没有特别的处理。

#3楼 @huacnlee 我被这玩意儿折磨的想死了= =!! 代码都一样,在RubyChina那里就可以,我那个就不行。。。

#3楼 @huacnlee 华顺哥能否分享一下mongoid和sunspot 的使用方法?

我现在还没心得,用的还很初级, @quakewang@saberma 好像用了很久了,呼唤他们来分享

我们就是按照文档上来的: 在Topic modle上定义一下searchable 然后在controller里面调用search @topics = Topic.search do keywords params[:query] end.results

不过用的是activerecord,不是mongoid

我也是用的activerecord

#5楼 @paranoyang 你试试把 vendor/plugins/sunspot_mongoid/lib/sunspot/mongoid.rb 的内容换为ruby-china的看看 https://github.com/huacnlee/ruby-china/blob/master/app/models/sunspot/mongoid.rb

我这个他不能直接用,我是 Integer ID 的

我连search 都不能直接用,要换成solr_search,刚发去了StackOverFlow...看看有没有老外知道吧。。。 http://stackoverflow.com/questions/8654329/how-to-make-sunspot-works-with-mongoid-rails-3

#11楼 @paranoyang 非常感谢, 我也遇到相同的问题, 从 stackoverflow 转到这里, 终于解决了! happy!

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册