如何使 Sunspot 与 Mongoid 一起工作 +轨道3
我正在使用 Rails3/Mongoid 编写一个类似博客的应用程序,现在尝试使用 Sunspot 进行全文搜索,
我按照以下步骤操作: https://github.com/outoftime/sunspot ,安装这个 https://github.com/jugyo/sunspot_mongoid 作为插件,并添加以下代码以使 sunspot:reindex
正常工作,
module Mongoid
module BaseModel
extend ActiveSupport::Concern
included do
scope :recent, desc(:_id)
scope :exclude_ids, Proc.new { |ids| where(:_id.nin => ids.map(&:to_i)) }
end
module ClassMethods
# like ActiveRecord find_by_id
#def find_by_id(id)
# if id.is_a?(Integer) or id.is_a?(String)
# where(:_id => id.to_i).first
# else
# nil
# end
#end
def find_in_batches(opts = {})
batch_size = opts[:batch_size] || 1000
start = opts.delete(:start).to_i || 0
objects = self.limit(batch_size).skip(start)
t = Time.new
while objects.any?
yield objects
start += batch_size
# Rails.logger.debug("processed #{start} records in #{Time.new - t} seconds") if Rails.logger.debug?
break if objects.size < batch_size
objects = self.limit(batch_size).skip(start)
end
end
end
end
end
并在我的帖子模型中:
include Sunspot::Mongoid
searchable do
text :title, :stored => true
text :content, :stored => true
text :comments, :stored => true do
comments.map { |comment| comment.content }
end
time :last_comment_at
end
但是当我使用 @search = Post.search
时,它总是给我这个错误..
Mongoid::Errors::InvalidFind in SearchController#index
Calling Document#find with nil is invalid
我发现它可能与 Mongoid 或其他东西冲突,并将其更改为 Post.solr_search
但是还是不行:
[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 >
谁能帮帮我吗?
I was writing a Blog like application Using Rails3/Mongoid, and now trying to use Sunspot for full-text search
I follow these steps: https://github.com/outoftime/sunspot , install this https://github.com/jugyo/sunspot_mongoid as plugin, and add the following code to make the sunspot:reindex
work correctly,
module Mongoid
module BaseModel
extend ActiveSupport::Concern
included do
scope :recent, desc(:_id)
scope :exclude_ids, Proc.new { |ids| where(:_id.nin => ids.map(&:to_i)) }
end
module ClassMethods
# like ActiveRecord find_by_id
#def find_by_id(id)
# if id.is_a?(Integer) or id.is_a?(String)
# where(:_id => id.to_i).first
# else
# nil
# end
#end
def find_in_batches(opts = {})
batch_size = opts[:batch_size] || 1000
start = opts.delete(:start).to_i || 0
objects = self.limit(batch_size).skip(start)
t = Time.new
while objects.any?
yield objects
start += batch_size
# Rails.logger.debug("processed #{start} records in #{Time.new - t} seconds") if Rails.logger.debug?
break if objects.size < batch_size
objects = self.limit(batch_size).skip(start)
end
end
end
end
end
and in my post model :
include Sunspot::Mongoid
searchable do
text :title, :stored => true
text :content, :stored => true
text :comments, :stored => true do
comments.map { |comment| comment.content }
end
time :last_comment_at
end
but when i use @search = Post.search
it always give me this error ..
Mongoid::Errors::InvalidFind in SearchController#index
Calling Document#find with nil is invalid
I find it may be conflict to Mongoid or something,and change it to Post.solr_search
but it still can't work :
[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 >
anyone can help me ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是非常测试版的。我试图让最简单的示例与 sunspot_mongoid 一起使用,但没有成功。不包含 rake 任务,可能与 github 上的此问题相关:
https://github.com/jugyo /sunspot_mongoid/issues/7
也许如果您在那里提出问题,维护人员将能够查看并做出回应。
This is very beta. I tried to make the simplest example work with sunspot_mongoid and had no luck. No rake tasks included, might be related to this issue on github:
https://github.com/jugyo/sunspot_mongoid/issues/7
Maybe if you file an issue over there the maintainer will be able to take a look and respond.
我已经更新了
sunspot_mongoid
,现在它工作得很好:D https://github.com/ hlegius/sunspot_mongoid2I've updated
sunspot_mongoid
and now it works nice :D https://github.com/hlegius/sunspot_mongoid2在我的应用程序中,我仅使用 sunspot_rails gem。这是我的太阳黑子配置文件:
In my application, I use only sunspot_rails gem. Here is my Sunspot config file:
如果您仍然想将 sunspot 与 mongo 一起使用,您可以使用 sunspot_mongo (与 mongo mapper 和 mongoid 一起使用)
https://github.com/derekharmel/sunspot_mongo
If you still want to use sunspot with mongo you can use sunspot_mongo (works with mongo mapper & mongoid)
https://github.com/derekharmel/sunspot_mongo
该项目有 4 个活跃的分叉。
您可以在此处查看所有 70 个分叉并选择您最喜欢的一个:
https://github.com/jugyo/ sunspot_mongoid/network
我目前正在使用 hlegius 提到的新 sunspot_mongoid2 gem(谢谢!)
There are 4 active forks of the project.
You can see all 70 forks and choose which one you like best here:
https://github.com/jugyo/sunspot_mongoid/network
I'm currently using the new sunspot_mongoid2 gem mentioned by hlegius (thank you!)
我可能会补充一点,有一个名为 sunspot_mongoid2 的新宝石,它对我有用,而 sunspot_mongoid 严重失败。
我还没有查看代码本身,但从 GitHub 来看,sunspot_mongoid 似乎已经有一段时间没有收到任何相关更新了。
I might add that there is a new gem called sunspot_mongoid2 that worked for me where sunspot_mongoid was failing terribly.
I haven't looked at the code itself, but from GitHub it seems sunspot_mongoid has not received any relevant updates for quite some time.