如何更新 django-haystack 的单个记录?

How to update a single record for django-haystack?

我是 haystack 初学者,我正在尝试了解如何更新文档。

我有以下 SearchIndex:

class ProductIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True) # contains keywords associated with the product
    name= indexes.CharField(model_attr='name')

    def get_model(self):
        return Product

    def prepare_text(self, obj):
        return [tag.keyword for tag in obj.productkeywords_set.all()]

我想在用户添加新产品关键字时更新 text 字段。我有超过80k条记录,所以我使用python manage.py update_index时需要很长时间。有没有办法只更新 one 文档?

你可以使用信号处理器https://django-haystack.readthedocs.io/en/master/signal_processors.html#realtime-realtimesignalprocessor

The other included SignalProcessor is the haystack.signals.RealtimeSignalProcessor class. It is an extremely thin extension of the BaseSignalProcessor class, differing only in that in implements the setup/teardown methods, tying ANY Model save/delete to the signal processor.

If the model has an associated SearchIndex, the RealtimeSignalProcessor will then trigger an update/delete of that model instance within the search index proper.

Configuration looks like:

HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

This causes all SearchIndex classes to work in a realtime fashion.

通过启用它,您的索引将在模型save/delete上自动更新