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

Custom bulk indexer for searchkick : mapping options are ignored

我正在使用 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