I've been looking through the Haystack documentation on multiple indexes, but I can't figure out exactly how to use them.
The main model in this example is Proposal
. I want to have two search indexes that return a list of proposals: one that only searches in the proposals themselves, and one that searches in proposals along with their comments. I've set up search_indexes.py
like this:
class ProposalIndexBase(indexes.SearchIndex, indexes.Indexable)
title = indexes.CharField(model_attr="title", boost=1.1)
text = indexes.NgramField(document=True, use_template=True)
date = indexes.DateTimeField(model_attr='createdAt')
def get_model(self):
return Proposal
class ProposalIndex(ProposalIndexBase):
comments = indexes.MultiValueField()
def prepare_comments(self, object):
return [comment.text for comment in object.comments.all()]
class SimilarProposalIndex(ProposalIndexBase):
pass
Here's my search in views.py
:
def search(request):
if request.method == "GET":
if "q" in request.GET:
query = str(request.GET.get("q"))
results = SearchQuerySet().all().filter(content=query)
return render(request, "search/search.html", {"results": results})
How do I set up a separate view that gets a SearchQuerySet from a specific index?
Comment
to yoursearch_models
list, will that return both proposals and comments in the search results? I think I was unclear in my question: I meant that the search results should always return only proposals, but in one case it should index just the proposal, in the other case it should index both the proposal and its comments. – Burtburta