gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:05:27 26 4
gpt4 key购买 nike

我是一个 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 时需要很长时间。有没有办法只更新一个文档?

最佳答案

您可以使用信号处理器 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.

通过启用它,您的索引将在模型上保存/删除时自动更新

关于python - 如何更新 django-haystack 的单个记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44641948/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com