我正在使用Searchkick 3.1.0
我必须对某些记录集合进行批量索引。根据我在文档中阅读并尝试过的内容,我无法将预定义的ID数组传递给Searchkick的reindex方法。我正在使用异步模式。
例如,如果执行Klass.reindex(async:true),它将在您的选项中将具有指定batch_size的作业排入队列。循环遍历整个模型的id的问题将确定是否必须对其进行索引。例如,如果我的数据库中有10000条记录,批处理大小为200,则它将排队50个工作。然后,它将在每个id上循环,如果满足search_import的条件,它将对其进行索引。
此步骤无用,我想加入一个预先过滤的ID数组,以防止遍历整个记录。
我尝试编写以下作业来覆盖正常行为:
def perform(class_name, batch_size = 100, offset = 0)
model = class_name.constantize
ids = model
.joins(:user)
.where(user: { active: true, id: $rollout.get(:searchkick).users })
.where("#{class_name.downcase.pluralize}.id > ?", offset)
.pluck(:id)
until ids.empty?
ids_to_enqueue = ids.shift(batch_size)
Searchkick::BulkReindexJob.perform_later(
class_name: model.name,
record_ids: ids_to_enqueue
)
end
问题:将记录插入ElasticSearch时,将完全忽略searchkick映射选项,我不知道为什么。不需要指定的匹配项(text_middle)并使用默认匹配项“关键字”创建映射。
是否有任何干净的方法可以批量重新索引一组记录,而不必使包含不需要的记录的作业入队?
答案 0 :(得分:0)
您应该能够根据以下条件为记录重新编制索引:
来自searchkick文档:
Reindex multiple records
Product.where(store_id: 1).reindex
您可以将它放在自己的延迟工作中。
我已经完成了一些在延迟作业中已经发生的批处理操作,我将代码包装在作业中的块中,也包含在searchkick文档中。
Searchkick.callbacks(:bulk) do
... // wrap some batch operations on model instrumented with searchkick.
// the bulk block should be outside of any transaction block
end