3

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

Rani
  • 6,424
  • 1
  • 23
  • 31

1 Answers1

6

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.

solarissmoke
  • 30,039
  • 14
  • 71
  • 73
  • You're absolutely right, but I tried both and still no success. Maybe I did something wrong... I create a file called blog_rebuild.py with the import and `class Command(rebuild_index.Command): pass` – Rani Jul 20 '16 at 05:59
  • You need to do the same for `update_index` - this is called by Haystack's `rebuild_index`. – solarissmoke Jul 20 '16 at 06:01
  • It works! my blog_rebuild still doesn't do anything but the blog_update_index.py works. Is it OK? – Rani Jul 20 '16 at 06:05
  • It's progress! You should be able to get blog_rebuild to work as well though using the same approach. Can't comment without seeing the full code of that file. – solarissmoke Jul 20 '16 at 06:06
  • I have a new problem now - it doesn't find the blog_text.txt although it's located in templates/search/indexes/myapp/ – Rani Jul 20 '16 at 06:08
  • Found the bug - the app name (e.g. myapp) should be the one I defined the model in and not where I defined the SearchIndex class – Rani Jul 20 '16 at 06:17
  • 1
    In Django 1.9, `haystack` needs to come **before** `wagtail.*` in `INSTALLED_APPS`, not after it. – Ilya Semenov Sep 19 '16 at 12:50