0

I am attempting to setup a search function on my django site using django-haystack with xapian backend. I followed the directions as per: http://django-haystack.readthedocs.org/en/latest/tutorial.html

When I enter a search it throws the error: Unable to open index at search/xapian/xapian_index

It seems that no search index was created when I ran ./manage.py rebuild_index However, no errors were reported at that time.

I am attempting to index the following model in myapp/models.py:

class MyMsg (models.Model):
    msg = models.TextField(max_length=2000)
    pub_date = models.DateTimeField('date published')
    author = models.ForeignKey(User)
    def __unicode__(self):
        return self.msg

I have the following search index in myapp/search_index.py:

class MyMsgIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    author = indexes.CharField(model_attr='author')
    pub_date = indexes.DateTimeField(model_attr='pub_date')

    def get_model(self):
        return MyMsg

    def index_queryset(self):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())

I am using: haystack 1.2.4 xapian 1.2.12 mac OS X 10.6.8

Thanks in advance for you help.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Roland
  • 103
  • 1
  • 1
  • 7
  • When you run `rebuild_index` do you see statistics about how many models it has indexed? I noticed the tutorial doesn't have a step about explicitly creating any model objects. – Brian Neal Aug 02 '12 at 01:42
  • The output from python manage.py rebuild_index is:WARNING: This will irreparably remove EVERYTHING from your search index in connection 'default'. Your choices after this are to restore from backups or rebuild via the `rebuild_index` command. Are you sure you wish to continue? [y/N] y Removing all documents from your index because you said so. All documents removed. – Roland Aug 02 '12 at 01:56
  • That's it? Well did you create any `Note` objects (or whatever model you chose to index)? – Brian Neal Aug 02 '12 at 01:58
  • I'm guessing that you don't have any `Note` objects in your database, and thus `rebuild_index` didn't find any work to do, and thus your xapian index didn't get built. – Brian Neal Aug 02 '12 at 02:15
  • I didn't see any reference to note objects. Can you explain? – Roland Aug 02 '12 at 02:46
  • Do you mean this type of index object (taken from haystack tutorial)? `class NoteIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) author = indexes.CharField(model_attr='user') pub_date = indexes.DateTimeField(model_attr='pub_date') def get_model(self): return Note def index_queryset(self): """Used when the entire index for model is updated.""" return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())` – Roland Aug 02 '12 at 02:51
  • I do have a search index for my model that I would like to make searchable. – Roland Aug 02 '12 at 02:56
  • I thought you were following the tutorial, which has a `Note` model. If you are indexing your own model, and you have objects in your database for that model, you will have to provide more information before we can help you. – Brian Neal Aug 02 '12 at 12:55
  • The Note model is just an example. The reader is meant to user their own model. By follow I meant as a guide to get search working with my site. – Roland Aug 02 '12 at 23:21
  • Gotcha. Thanks for adding more information. – Brian Neal Aug 03 '12 at 00:17

1 Answers1

0

You say you are using Haystack 1.2.4, but you linked to the new 2.x beta documentation. In earlier versions of Haystack you need to add an "auto discover" step.

It involves creating a variable in settings.py called HAYSTACK_SITECONF that points to a haystack configuration module. Inside that module you need to have at least these lines:

import haystack
haystack.autodiscover()

See the tutorial for your version: http://django-haystack.readthedocs.org/en/v1.2.4/tutorial.html

Could this be the problem?

Brian Neal
  • 31,821
  • 7
  • 55
  • 59
  • Thanks for pointing out the inconsistency. I didn't notice it. I tried adding the HAYSTACK_SITECONF to my settings.py and autodiscover but this message: `django.core.exceptions.ImproperlyConfigured: The HAYSTACK_SITECONF setting is no longer used & can be removed.` and xapian_index was still not created. – Roland Aug 03 '12 at 00:45
  • Do you know what version of Haystack you have? – Brian Neal Aug 03 '12 at 01:00
  • The downloaded file has `v1.2.0` in the name (not sure where I saw 1.2.4) but `haystack/__init__.py` has the line `__version__ = (2, 0, 0, 'beta')` which explains why `HAYSTACK_SITECONF` flagged an error. Does ver 2.0.0 work or should I revert to an older version? – Roland Aug 03 '12 at 02:10
  • I don't have experience with 2.x, but it should work. I use a 1.2.x version with Xapian and it runs great for me. I don't think we can get to the bottom of this on StackOverflow. We could try some things in chat or on IRC. The Haystack author provides great support in IRC (freenode) in the #haystack channel. – Brian Neal Aug 03 '12 at 12:45
  • I posted the question at https://groups.google.com/forum/?fromgroups#!topic/django-haystack/SY7E1e2qZ-c – Roland Aug 05 '12 at 03:49