Search code examples
djangoindexingdjango-haystackwhoosh

Django-Haystack + Whoosh - empty index after rebuild_index


I'm trying to use Haystack with Whoosh to index and search in my app. When I'm rebuilding the index I'm getting this results:

All documents removed. Updating backend: default default: Backend doesn't require rebuild. Skipping

this is my SearchIndex Class:

class BlogIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True, template_name="snip_text.txt")
    headline = indexes.CharField(model_attr="headline", null=True)
    body = indexes.CharField(model_attr="body")


    def get_model(self):
        return Snip

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(date__lte=timezone.now())

This is my blog_text.txt file (which located in templates/search/indexes/myapp/):

{{ object.headline }}
{{ object.body }}

I added haystack to INSTALLED_APPS and its configuration in the settings file. My DB is sqlite (just for development...).

What am I doing wrong?

Thanks!

R

UPDATE

Create a management command like this (name the file as you wish - e.g my_update_index.py)

from haystack.management.commands import update_index

class Command(update_index.Command):
    pass

Do the same for the clear_index command.

the rebuild_index command calls clear_index and the update_index, therefore even if you'll create a new rebuild command it won't work (cause it's looking for the wrong commands).

Just run both commands when you want to rebuild the index, otherwise run your update_index command.

One more note: the name of the folder of the template txt file has to be exactly the same as the model you're trying to index (and it's doesn't matter where in the Index Class you wrote apparently...).

And of course, credit to @solarissmoke


Solution

  • I am betting that you have Wagtail installed in your project as well - because that is where the Backend doesn't require rebuild is coming from.

    The problem is that Wagtail defines its own update_index management command which conflicts with the one that Haystack has (rebuild_index calls update_index). When you try to rebuild your index, the Wagtail command is being called instead of the Haystack one.

    The quick and dirty solution is to make sure that haystack comes after wagtail in INSTALLED_APPS - its commands will be loaded last and will replace the Wagtail ones.

    Alternative you need to write your own management command that wraps Haystack's rebuild_index code.