我正在使用Searchkick 3.1.0
我必须批量索引某些记录集合。通过我在文档中阅读并尝试过的内容,我无法将预定义的数组ID传递给Searchkick的reindex方法。我正在使用异步模式。
例如,如果您使用Klass.reindex(async:true),它会在您的选项中使用指定的batch_size将作业排入队列。它遍历整个模型的id的问题将确定它们是否必须被索引。例如,如果我的数据库中有10 000条记录,批量大小为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)并使用默认匹配'keyword'创建映射。
是否有任何干净的方法来批量重新索引记录数组而不必排队包含不需要的记录的作业?
您应该能够根据条件重新索引记录:
来自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