提问者:小点点

searchkick的自定义批量索引器:映射选项被忽略


我正在使用Searchkit3.1.0

我必须对某个记录集合进行批量索引。根据我在文档中阅读并尝试过的内容,我无法将预定义的id数组传递给Searchkick的reindex方法。我正在使用异步模式。

例如,如果执行Klass.rendex(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)并使用默认匹配“keyword”创建映射。

是否有任何干净的方法来批量重新索引记录数组,而不必将包含不需要的记录的作业排队?


共1个答案

匿名用户

您应该能够根据条件重新索引记录:

从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