2016-12-29 46 views
0

要追蹤獨特視圖,我添加了impressionist gem。由於流量較高,展示次數表格將以更快的速度增長。當我想要顯示連續的周/月之間的比較時,這會導致問題。無法將印象派模型添加到Solr

所以我想連接impression's模型與solr

爲了實現這個功能,我首先創建了一個名爲的印模下的app/models/impression.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 

然後,我看了幾個博客帖子印象派實施,然後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' 

誰能告訴我怎麼可以創建一個印象模型,使用它與Solr的

回答

0

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 

,我能夠到該文件中定義自己的類和實例方法。