无法将印象派模型添加到Solr
为了跟踪独特的观点,我添加了印象派gem 。 由于流量较高, 展示次数表会以更快的速度增长。 当我想要显示连续几周/几个月之间的比较时,这将导致问题。
所以我想到将impression's
模型与solr联系起来。
为了实现这一点,我首先在app/models/impression.rb
文件夹下创建了一个名为impression的 app/models/impression.rb
。
class Impression [ :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
然后我查看了一些实施印象派的博客文章,然后进行了重新索引。
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 `' 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
我能够在此文件中定义自己的类和实例方法。