0

为了跟踪独特的观点,我添加了印象派 gem。由于更高的流量,展示次数表将以更快的速度增长。当我想显示连续周/月之间的比较时,这将在以后引起问题。

所以我想到了将impression'smodel 与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 重新索引。

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一起使用的印象模型?

4

1 回答 1

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

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

于 2017-01-14T14:15:00.720 回答