I am new to Haystack and I just installed it using Whoosh .
base.py
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
},
}
My ad model (located in the app posts):
class Announcement(models.Model):
title = models.CharField(null=True, max_length=256)
description = models.CharField('Announcement description',
null=True, max_length=500)
slug = models.SlugField(default=uuid.uuid1, unique=True)
author = models.ForeignKey(to=User, related_name='posts',
null=True, blank=True)
So, I built an index file with a name search_indexes.pylocated inside the application posts.
class AnnouncementIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
title = indexes.CharField(model_attr="title")
description = indexes.CharField(model_attr="description")
author = indexes.CharField(model_attr='author')
status = indexes.BooleanField(model_attr='status')
def get_model(self):
return Announcement
def index_queryset(self, using=Announcement):
return self.get_model().objects.filter(status=False)
My search and the search form do not fit in the application posts, they are in the application with the name homepages. This is the form homepages/forms.py:
class PostsSearchForm(SearchForm):
def no_query_found(self):
return self.searchqueryset.all()
And the view homepages/views.py:
def home(request):
categories = Category.objects.all()
if request.user.is_authenticated():
current_user = request.user
form = PostsSearchForm(request.GET)
posts = None
if form.data != {} and form.is_valid():
posts = form.search()
return render_to_response('search/search.html', {
'posts': posts,
'form': form
})
The template with the form is in the file templates/homepages/home.html:
<form method="GET">
{% csrf_token %}
{{ form.q }}
</form>
And I have a file templates/searches/indexes/posts/announcement_text.txt:
{{ object.title }}
{{ object.description }}