Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-4elasticsearchsearchkick

Rails4 - how to customize the indexed data while indexing in Elastic Search using SearchKick


i have recently started using ElasticSearch along with searchkick gem to index my db data.By default, searchkick gem will index all columns of a model but i want to index only active items(like posts which are published).For example -

#####in shop.rb

has_many :dealers
has_one :address
has_one :contact
...blah blah and more has many

###my index data


   def search_data
    { 
      ##XXXXXXXX  why cant i use this XXXXXXXXX
      ###active: true,
      name: name, index: "not_analyzed", 
      capacity: capacity.to_i,
      slug: slug,
      ### i need to add active to so that i can use where(:active => true) in search
      active: active,
      event_type_name: event_types.map(&:name),
      facility_name: facilities.map(&:name),
      ratings: ratings.map(&:stars),
      location: [self.address.latitude, self.address.longitude],
      picture_url: pictures.select{|p| p == pictures.last}.map do |i|{

        original:  i.original_url 

        }
      end
    }
  end

is the above indexing correct? how can i index only those shops whose attribute value for active is true.?


Solution

  • According to the docs: https://github.com/ankane/searchkick

    There is a should_index? method that looks like exactly what you need:

    By default, all records are indexed. To control which records are indexed, use the should_index? method together with the search_import scope.

    class Product < ApplicationRecord
      scope :search_import, -> { where(active: true) }
    
       def should_index?
         active # only index active records
       end
    end