开发者

Removing from a Haystack/Xapian Index with update_index --remove

I'm trying to have the ./manage.py update_index --remove management command remove results from the search index.

I'd like to remove the objects, not when they are deleted, just when their:

enabled = models.BooleanField()

field is False

What do I do here? Do I need to prepare the SearchIndex in anyway?

import datetime
from haystack.indexes import *
from haystack import site
from articles.models import Article

class ArticleIndex(SearchInd开发者_如何转开发ex):

    text = CharField(document=True, use_template=True)
    title = CharField(model_attr='title')
    content = CharField(model_attr='content')

    def get_queryset(self):
        """Used when the entire index for model is updated."""
        return Article.site_published_objects.filter(enabled=True)

    def get_updated_field(self):
        return 'modified'

    def remove_object(self):
        pass

site.register(Article, ArticleIndex)

Thank you.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜