Search code examples
pythondjangofull-text-searchdjango-haystackwhoosh

Categorize results based on Model in haystack?


When we search using the SearchQuerySet API provided by django-haystack the result is a list containing objects of model instances across all of the indexes.

Now for post computation I want to categorize search result based on the model from which they were fetched.

Normally I would do something like this

from haystack.query import SearchQuerySet
hello_results = SearchQuerySet().filter(content='hello')

I can iterate over the list of models and the use .models() to query specifically using a single index. But I think this approach will be much slower as multiple searches will have to be executed.

Is there a better approach to accomplish the same.

EDIT:

By this I mean:

Let there be two models A and B ,

Now by some means I want to know that which of the results were fetched from model A and which ones from Model B.


Solution

  • Each result has an attribute model_name. So you can use groupby in your view, or check result.model_name when looping through the result in your search results template.