Algolia 重新索引问题

在执行 algolia:reindex 的 rake 任务时,它崩溃并出现以下错误:

Clearing users from Algolia
Pushing users to Algolia
...
Successfully pushed 1849 users to Algolia
Clearing tags from Algolia
Pushing tags to Algolia
..
Successfully pushed 53 tags to Algolia
Clearing posts from Algolia
Pushing posts to Algolia
rake aborted!
Algolia::AlgoliaHttpError: Record at the position 662 objectID=690 is too big size=20920/20000 bytes. Please have a look at https://www.algolia.com/doc/guides/sending-and-managing-data/prepare-your-data/in-depth/index-and-records-size-and-usage-limitations/#record-size-limits (Algolia::AlgoliaHttpError)

据我所知,该插件不包含将较长的帖子拆分为单独块的功能。我通过在 @index.save_objects 调用之前添加

objects.reject! { |object| object.to_json.bytesize > 20000 }

来规避这个问题,但这同时也意味着这些帖子根本没有被索引。

2 个赞

API期望什么?我们应该截断还是分块发送?

根据 https://www.algolia.com/doc/guides/sending-and-managing-data/prepare-your-data/how-to/indexing-long-documents/,需要将其分块,并在搜索时将 distinct 设置为 true。

最大尺寸取决于套餐,但我找不到查询它的方法。考虑到某些套餐还有“所有记录的平均记录大小”限制,最好分块为 10 KB 10000 字节。

2 个赞

修复“记录过大”导致 Algolia 帖子重新索引中断的问题

我遇到一个问题,Algolia 中只包含了我论坛帖子的一小部分。运行帖子重新索引后揭示了原因:

Algolia::AlgoliaHttpError:
Record at the position 258 objectID=1006 is too big:
size: 108426 byte(s), maximum allowed: 10000 byte(s).

在我安装的插件版本中,PostIndexer#to_object 会将整个帖子的纯文本作为单条记录发送,而不会检查序列化记录的大小。过大的记录导致重新索引任务中止。

以下本地解决方法使得重新索引能够完成。

此补丁的作用

该补丁将每个完整序列化的帖子记录限制为 9,500 字节,在 Algolia 报告的 10,000 字节限制之下留出一定余量。

它:

  • 在大小计算中包含元数据和 JSON 转义字符。

  • 仅截断发送到 Algolia 的 content。

  • 保留原始的 Discourse 帖子不变。

  • 保持现有的对象 ID 和每帖一条记录的结构。

权衡之处在于,从长帖子末尾省略的文本将无法通过 Algolia 搜索到。这是截断,而非文档分块。

补丁

在:

plugins/discourse-algolia/lib/discourse_algolia/post_indexer.rb

找到 to_object 中的最终 return 语句,它紧接在可选的分类元数据添加之后:

    object
  end

将该部分替换为:

# 限制完整的序列化记录,而不仅仅是其内容。
    algolia_record_budget = 9_500

    if object.to_json.bytesize > algolia_record_budget
      full_content = object[:content]
      object[:content] = ""

      if object.to_json.bytesize > algolia_record_budget
        raise "Algolia metadata alone exceeds the record budget for post #{post.id}"
      end

      low = 0
      high = full_content.length
      best_content = ""

      while low <= high
        midpoint = (low + high) / 2
        object[:content] = full_content[0, midpoint]

        if object.to_json.bytesize <= algolia_record_budget
          best_content = object[:content]
          low = midpoint + 1
        else
          high = midpoint - 1
        end
      end

      object[:content] = best_content
    end

    object
  end

二分查找旨在找到一个在 JSON 序列化后符合字节预算的内容前缀。如果仅元数据就超出预算,补丁将抛出明确的错误,而不是静默丢弃元数据。

编辑前请备份文件。此补丁针对 to_object 返回包含 content 的单个哈希的版本;在应用之前请根据您的安装版本进行审查。

验证并重新索引

对于标准 Docker 安装,请从主机进入容器:

cd /var/discourse
./launcher enter app

切换到 Discourse 用户:

su - discourse

当出现 discourse 用户提示符后,运行:

cd /var/www/discourse
ruby -c plugins/discourse-algolia/lib/discourse_algolia/post_indexer.rb

如果报告 Syntax OK,请运行:

RAILS_ENV=production LOAD_PLUGINS=1 bundle exec rails algolia:reindex_posts

此任务会清除并重新填充帖子索引。就我的情况而言,应用补丁后它成功完成了。

正在运行的应用程序/后台工作进程也需要重启,以便在持续索引时使用打过补丁的代码。

另一个注意点:报告的帖子数量

任务报告:

Successfully pushed 723 posts to Algolia
But Algolia contained 272 records.

但 Algolia 中只包含 272 条记录。

在我安装的版本中,任务在调用索引器的可见性过滤器之前就会增加其计数器。使用相同索引器进行的诊断确认:

Posts examined:         723
Eligible for indexing:  272
Excluded by visibility: 451

因此,成功消息统计的是已检查的帖子数量,而不一定是上传的记录数。这种差异与记录过大错误是分开的。