ruby-on-rails - Rails4 - 如何在使用 SearchKick 在 Elastic Search 中进行索引时自定义索引数据
问题描述
我最近开始使用 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 的商店。?
解决方案
根据文档: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
推荐阅读
- spring-boot - 在zuul后面运行的Springboot微服务没有设置cookies
- paypal - Paypal - 在 php 中获取 onApprove 获取变量
- reactjs - React redux Too many re-renders.React 限制渲染次数防止死循环
- c# - 检索属性的名称和值具有特定的属性值
- python - Python将字符串列表转换为python列表
- scala - 如何在 Scala 的编译时验证类型是否存在
- python - 字符串python中的多个分隔符
- python - SQLAlchemy - 继承将属性条目写入两个表
- arrays - Avro 模式可以指定最小数组长度吗?
- python-3.x - 如何在 python 中创建列表和添加元素?