【问题标题】:Rails4 - how to customize the indexed data while indexing in Elastic Search using SearchKickRails4 - 如何在使用 SearchKick 在 Elastic Search 中进行索引时自定义索引数据
【发布时间】:2018-04-26 21:52:50
【问题描述】:

我最近开始使用 ElasticSearch 和 searchkick gem 来索引我的数据库数据。默认情况下,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 的商店?

【问题讨论】:

  • 为什么它被否决了?这个问题没有意义吗?

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 elasticsearch searchkick


【解决方案1】:

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

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

默认情况下,所有记录都被索引。控制哪些记录是 索引,使用 should_index?方法与 search_import 范围。

class Product < ApplicationRecord
  scope :search_import, -> { where(active: true) }

   def should_index?
     active # only index active records
   end
end

【讨论】:

  • 所以我可以说如果我将它与我的 search_data 块一起使用,然后运行 ​​Reindex..我应该只获得活动项目?
  • 我只是在使用我刚刚在文档中读到的内容,所以我无法确认或否认,不幸的是......对不起:(但这似乎是你需要的。
  • 像魅力一样工作..谢谢
猜你喜欢
  • 2023-03-09
  • 1970-01-01
  • 2012-02-15
  • 2023-01-13
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多