Skip to main content
 首页 » 编程设计

python之添加新实体后的 Django haystack 索引

2025年05月04日24飞鱼

我想知道在将新实体添加到数据库后应该调用什么操作才能使这个实体可以用 haystack 搜索:

  • 我应该只更新索引吗?
  • 我应该重建整个索引吗?

有问题的是,新实体将被频繁添加,并且数据库中可能存在大量实体。

请您参考如下方法:

如果您要向数据库中添加新行,那么 update_index 就足够了。

来自haystack docs :

The conventional method is to use SearchIndex in combination with cron jobs. Running a ./manage.py update_index every couple hours will keep your data in sync within that timeframe and will handle the updates in a very efficient batch.

如果您向搜索索引添加了一个新的字段,那么您需要运行rebuild_index:

If you have an existing SearchIndex and you add a new field to it, Haystack will add this new data on any updates it sees after that point. However, this will not populate the existing data you already have.

In order for the data to be picked up, you will need to run ./manage.py rebuild_index. This will cause all backends to rebuild the existing data already present in the quickest and most efficient way.