Search code examples
pythondjangodjango-haystack

Getting raw results from Django Haystack


I have site using Django Haystack (with the Whoosh backend). I would like to be able to get all results for a given model matching a given query. Something like

from haystack import get_results

result_list = get_results(model=MyModel, query='foo')

In the documentation I have found a lot of stuff about customizing the default views and forms, advanced searching and so on, but I cannot find anything for the simple task of getting all the models matching a query and managing them on my own. Is this possible?


Solution

  • You can use SearchQuerySet. For example:

    In [1]: from haystack.query import SearchQuerySet
    
    In [2]: SearchQuerySet().filter(content='abra')
    Out[2]: [<SearchResult: art.artist (pk=u'23')>, <SearchResult: art.artwork (pk=u'191')>]
    
    In [3]: SearchQuerySet().filter(content='abra').count()
    Out[3]: 2