[Django]-Removing from a Haystack/Xapian Index with update_index –remove

7👍

I haven’t tested this, but you might be able to achieve the desired effect by wrapping your search index template in a condition, e.g.:

{# in search/indexes/yourapp/article_text.txt #}
{% if object.enabled %}
    {# ... whatever you have now #}
{% endif %}

When you run ./manage.py update_index, articles with enabled=False will end up with no data associated to it and not show up in searches.

Update

Looking at the source for SearchIndex, there a remove_object() method for removing an object from the index. There’s also the should_update() which is run to determine if an object’s index should be updated.

Perhaps it’s possible to trigger the index removal using something like:

class ArticleIndex(SearchIndex):
    # ...

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

Leave a comment