首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让太阳黑子与Mongoid + Rails 3协同工作

如何让太阳黑子与Mongoid + Rails 3协同工作
EN

Stack Overflow用户
提问于 2011-12-28 17:55:35
回答 6查看 3.7K关注 0票数 0

我使用Rails3/Mongoid编写了一个类似博客的应用程序,现在尝试使用Sunspot进行全文搜索

我遵循以下步骤:https://github.com/outoftime/sunspot,将此https://github.com/jugyo/sunspot_mongoid安装为插件,并添加以下代码以使sunspot:reindex正常工作。

代码语言:javascript
复制
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

在我的帖子模型中:

代码语言:javascript
复制
  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时,它总是给我这个错误。

代码语言:javascript
复制
Mongoid::Errors::InvalidFind in SearchController#index
Calling Document#find with nil is invalid

我发现它可能与Mongoid或其他东西冲突,并将其更改为Post.solr_search

但它仍然不能工作:

代码语言:javascript
复制
[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 > 

有人能帮我吗?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-01-04 07:05:35

这是非常测试版的。我试着用sunspot_mongoid做最简单的例子,但没有成功。没有包含rake任务,可能与github上的这个问题有关:

https://github.com/jugyo/sunspot_mongoid/issues/7

也许如果你在那里提交一个问题,维护者将能够看一看并做出响应。

票数 1
EN

Stack Overflow用户

发布于 2013-05-26 23:11:32

我已经更新了sunspot_mongoid,现在它工作得很好:D https://github.com/hlegius/sunspot_mongoid2

票数 1
EN

Stack Overflow用户

发布于 2012-02-09 05:36:50

在我的应用程序中,我只使用了sunspot_rails gem。以下是我的Sunspot配置文件:

代码语言:javascript
复制
Sunspot.config.pagination.default_per_page = 24

module MongoidAdapter
  class InstanceAdapter < Sunspot::Adapters::InstanceAdapter
    def id
      @instance.id.to_s
    end
  end

  class DataAccessor < Sunspot::Adapters::DataAccessor
    def load(id)
      @clazz.criteria.for_ids(BSON::ObjectId(id))
    end

    def load_all(ids)
      @clazz.criteria.in(:_id => ids.map {|id| BSON::ObjectId(id)})
    end
  end
end

Sunspot::Adapters::DataAccessor.register(MongoidAdapter::DataAccessor, Mongoid::Document)
Sunspot::Adapters::InstanceAdapter.register(MongoidAdapter::InstanceAdapter, Mongoid::Document)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8654329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档