Search code examples
pythondjangoelasticsearchdjango-haystack

How to update a single record for django-haystack?


I'm a haystack beginner, and I'm trying to figure out how to update a document.

I have the following 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()]

I want to update the text field when a user adds a new product keyword. I have over 80k records, so it takes a very long time when I use python manage.py update_index. Is there a way to update just one document?


Solution

  • You could use signal processor 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.

    By enabling it your index will be updated automatically on save/delete on the model