pythondjangodjango-haystack

Django-Haystack not finding any field


I am trying to write a little search engine with django-haystac and whoosh. I adapted their tutorial, I've created my index from a JSON file and query it successfully with QueryParser and now I'm trying to use their view.

when I try to access the search url at: http://127.0.0.1:8000/my_search/ I get the error:

The index 'PaperIndex' must have one (and only one) SearchField with document=True.

If I remove the search_indexes.py I can access the search page, but then of course it does not work as it doesn't have any indecies to search.

By debugging it seems it does not pickup any fields, but it does see the class.

I tried several things but nothing worked

my search_indexes.py:

from haystack import indexes
from my_search.models import Paper


class PaperIndex(indexes.SearchIndex, indexes.Indexable):
    """
    This is a search index
    """

    title = indexes.CharField(model_attr='title'),
    abstract = indexes.CharField(document=True,
       use_template=False, model_attr='abstract'),


    def get_model(self):
        return Paper

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

my models.py:

from django.db import models


class Paper(models.Model):
    paper_url = models.CharField(max_length=200),
    title = models.CharField(max_length=200),
    abstract = models.TextField()
    authors = models.CharField(max_length=200),
    date = models.DateTimeField(max_length=200),

    def __unicode__(self):
        return self.title

thanks!


Solution

  • Haystack uses an extra field for the document=True field. The text = indexes.CharField(document=True) is not on the model, and haystack dumps a bunch of search-able text in there.

    Haystack provides a helper method prepare_text() to populate this field. Alternatively, the template method can be used, which is simply a txt file with django template style model properties on it.

    class PaperIndex(indexes.SearchIndex, indexes.Indexable):
        """
        This is a search index
        """
    
        text = indexes.CharField(document=True)
    
        title = indexes.CharField(model_attr='title'),
        abstract = indexes.CharField(model_attr='abstract'),
    
        def get_model(self):
            return Paper
    
        def index_queryset(self, using=None):
            """Used when the entire index for model is updated."""
            return self.get_model().objects  # .filter(
                pub_date__lte=datetime.datetime.now())