简体   繁体   中英

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.

So I thought of linking impression's model with solr .

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

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.

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'

Can anyone tell me how can I create a Impression model for using it with solr ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM