无法将印象派模型添加到 Solr

Unable to add Impressionist model to Solr

为了跟踪我添加的独特观点 impressionist gem。由于流量增加,展示次数 table 将以更快的速度增长。当我想显示连续 weeks/months.

之间的比较时,这会在以后引起问题

所以我想到了将 impression's 模型与 solr 链接起来。

为了实现这个,我首先在 app/models/impression.rb 文件夹下创建了一个名为 impression 的模型。

class Impression < ActiveRecord::Base

    attr_accessible :user_id, :ip_address, :action_name, :controller_name, :impressionable_type, :impressionable_id, :view_name, :session_hash, :message, :request_hash, :referrer

    searchable :ignore_attribute_changes_of => [ :updated_at] do
        text :message, :boost=> 2.0
        text :referrer
        text :ip_address
        integer :user_id
        time :created_at
        time :updated_at       
        string :message
        string :view_name
        string :ip_address
        string :impressionable_type 
        string :controller_name
        string :action_name
        string :session_hash
        string :request_hash
        integer :id
        integer :impressionable_id   
      end
end

然后我看了几个实现印象派的博文,然后做了solr reindexing。

bundle exec rake sunspot:reindex[,Impression]
[###########################################################################################################] [4/4] [100.00%] [00:00] [00:00] [2.78/s]
# This did the reindexing.

接下来我尝试使用 solr 查询 Impression 模型。

search = Sunspot.search Impression do
      with(:impressionable_type).equal_to('Blogpost')
      with(:impressionable_id).equal_to(1)
    end.total
    puts "total results #{search || 0}"
search

上面的代码不断抛出这个错误:

undefined method `field' for nil:NilClass
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/dsl/standard_query.rb:112:in `with'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/util.rb:241:in `__proxy_method__'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/util.rb:236:in `method_missing'
from (irb):8:in `block in irb_binding'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/util.rb:208:in `instance_eval'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/util.rb:208:in `instance_eval_with_context'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/util.rb:86:in `instance_eval_or_call'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/search/abstract_search.rb:202:in `build'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/session.rb:50:in `new_search'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/session.rb:58:in `search'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `search'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-queue-0.10.2/lib/sunspot/queue/session_proxy.rb:62:in `search'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot.rb:345:in `search'
    from (irb):8
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/railties-3.2.14/lib/rails/commands/console.rb:47:in `start'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/railties-3.2.14/lib/rails/commands/console.rb:8:in `start'
    from /home/xyz/.rvm/gems/ruby-2.1.0/gems/railties-3.2.14/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'

谁能告诉我如何创建一个 Impression 模型以便与 solr 一起使用?

通过在 config/initializers/impressionist_patch.rb 下添加相同的模型定义来解决此问题:

class Impression < ActiveRecord::Base

    attr_accessible :user_id, :ip_address, :action_name, :controller_name, :impressionable_type, :impressionable_id, :view_name, :session_hash, :message, :request_hash, :referrer

    searchable :ignore_attribute_changes_of => [ :updated_at] do
        text :message, :boost=> 2.0
        text :referrer
        text :ip_address
        integer :user_id
        time :created_at
        time :updated_at       
        string :message
        string :view_name
        string :ip_address
        string :impressionable_type 
        string :controller_name
        string :action_name
        string :session_hash
        string :request_hash
        integer :id
        integer :impressionable_id   
      end
end

而且我能够在此文件中定义自己的 class 和实例方法。