无法将印象派模型添加到 Solr [英] Unable to add Impressionist model to Solr
问题描述
为了跟踪独特的观点,我添加了印象派宝石.由于流量增加,展示次数表将以更快的速度增长.当我想显示连续几周/几个月之间的比较时,这会导致问题.
To keep track of unique views I added impressionist gem. The impressions table will grow at a faster rate due to higher traffic. This will cause problems later when I want to show a comparison between consecutive weeks/months.
所以我想到了将 impression's
模型与 solr 联系起来.
So I thought of linking impression's
model with solr.
为了实现这一点,我首先在 app/models/impression.rb
文件夹下创建了一个名为 impression 的模型.
To implement this I first created a model named impression under app/models/impression.rb
folder.
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 重新索引.
Then I viewed few blogposts where impressionist was implemented and then did 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
模型.
Next I tried using solr to query Impression
model.
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
上面的代码不断抛出这个错误:
The above code keeps throwing this error:
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 一起使用?
Can anyone tell me how can I create a Impression model for using it with solr?
推荐答案
通过在 config/initializers/impressionist_patch.rb
下添加相同的模型定义来修复此问题:
Fixed this by adding the same model definitions under 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
而且我能够在这个文件中定义我自己的类和实例方法.
And I was able to define my own class and instance methods within this file.
这篇关于无法将印象派模型添加到 Solr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!