I'm using haystack for the full site search on my project which searches within books, authors, events, and videos models.
Then I have the main book page where I want to search only against the Books model.
I found this post: How to return only indexed objects of a specific type in Haystack
However it does not appear to be working for me. I'm testing locally using the simple backend and I know that does have some issues and i'm not sure if this related.
my search_indexes.py file looks like this:
class BookSearchIndex (SearchIndex):
text = CharField(document=True, use_template=True)
title_web = CharField(model_attr='title_web', boost=1.125)
on_sale_date = CharField(model_attr='on_sale_date')
def index_queryset(self):
return Book.objects.active().filter(publish_level='published')
site.register(Book, BookSearchIndex)
And in my view, if there was a search query passed, return only books with that query, otherwise show all books:
search = self.request.GET.get('search', None)
if search:
clean_query = SearchQuerySet().query.clean(search)
sqs = SearchQuerySet().models(Book).filter(content=clean_query).order_by('-on_sale_date')
else:
sqs = SearchQuerySet().models(Book).order_by('-on_sale_date)
The search correctly filters item based on the search query, but it's still returning all models. It's not limiting it to just the Book model.
It seems like this part has no effect:
SearchQuerySet().models(Book)
Can anyone help me figure out what I'm doing wrong?
I figured it out. I was trying to use the simple search for testing locally because we didn't have a solr backend setup yet.
Using the .models() function doesn't work with the simple backend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With