Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-Haystack: should_update doesn't appear to trigger with RealtimeSignalProcessor?

I have a model that has a boolean field that states whether the model object is active or not. I basically wish for Haystack to ignore any objects with active=False. This works fine for a complete reindex using:

def index_queryset(self, using=None):
    return ExampleModel.objects.filter(active=True)

However when an object is updated and the index is realtime updated and not completely reindexed, such as when changing an object to inactive, the following doesn't work and additionally seems to be uncalled:

def should_update(self, instance):
    if instance.active:
        return True
    self.remove_object(instance)
    return False

I want the object to be removed from the index when it is marked as inactive, however on updating the object to inactive it remains in the index affecting facet counts etc. I checked with using manage.py update_index and the should_update code doesnt seem to run?

For info i am using haystack dev and elasticsearch latest stable.

Any ideas?

like image 728
dave12345678 Avatar asked Dec 06 '25 11:12

dave12345678


1 Answers

Looking at the source code, should_update() returns True by default which means reindex. Additionally, remove_object() is attached to the class's post-delete hook, which could be why it's not being called since you're not deleting the record.

You should be able to trigger the index removal by slightly altering your code like this:

def should_update(self, instance, **kwargs):
    if instance.active:
        return True
    else:
        self.remove_object(instance, **kwargs)
        return False

or the reverse:

def should_update(self, instance, **kwargs):
    if not instance.active:
        self.remove_object(instance, **kwargs)
    return instance.active

Another option would be to create a CRON script that does:

import haystack
from yourapp.models import ExampleModel

for obj in ExampleModel.objects.filter(active=False):
    haystack.site.remove_object(obj)

This could also be used by the post_save signal in Django.

Fine print: I have not tested any of this code. It is theoretical based on the information provided in the question.

like image 84
Drewness Avatar answered Dec 08 '25 23:12

Drewness