Rails4 - 如何在使用 SearchKick 在 Elastic Search 中编制索引时自定义索引数据

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

我最近开始使用 ElasticSearch 和 searchkick gem 来索引我的数据库 data.By 默认情况下,searchkick gem 将索引模型的所有列,但我只想索引活动项目(如已发布的帖子)。例如 -

#####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

以上索引是否正确? 我如何才能仅索引那些 active 属性值为 true 的商店?

根据文档:https://github.com/ankane/searchkick

有一个 should_index? 方法看起来正是您所需要的:

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