Note that there are some explanatory texts on larger screens.

plurals
  1. POSolr search results with Django REST Framework
    primarykey
    data
    text
    <p>I'm using <a href="http://django-rest-framework.org/" rel="nofollow">Django REST Framework</a> for our web API and <a href="http://lucene.apache.org/solr/" rel="nofollow">Solr</a> to power a search. Currenlty, in a subclass of <code>ListAPIView</code>, I override <code>get_queryset()</code> to get a <code>QuerySet</code> with the Solr search results:</p> <pre><code>class ClipList(generics.ListAPIView): """ List all Clips. Permissions: IsAuthenticatedOrReadOnly Parameters: query -- Search for Clips. EX: clips/?query=aaron%20rodgers """ model = Clip serializer_class = ClipSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly,) def get_queryset(self): params = request.GET query = params.get('query', None) queryset = Clip.objects.all() if query is not None: conn = solr.Solr(settings.SOLR_URL) sh = solr.SearchHandler(conn, "/select") response = sh(query) ids = [] for result in response.results: ids.append(result['id']) # filter the initial queryset based on the Clip identifiers from the Solr response # PROBLEM: This does not preserve the order of the results as it equates to # `SELECT * FROM clips WHERE id in (75, 89, 106, 45)`. # SQL is not guaranteed to return the results in the same order used in the WHERE clause. # There is no way (that I'm aware of) to do this in SQL. queryset = queryset.filter(pk__in=ids) return queryset </code></pre> <p>However, as explained in the comments, this does not preserve the order of the results. I realize I could make a Python set of Clip objects, but I would then lose the lazy-evaluation of Django <code>QuerySet</code> and the results are likely to be large and will be paginated.</p> <p>I looked into <a href="http://haystacksearch.org/" rel="nofollow">Haystack</a>, and my understanding is that the code above using Haystack would look like:</p> <pre><code> def get_queryset(self): params = self.request.GET query = params.get('query', None) search_queryset = SearchQuerySet().filter(content=query) return search_queryset </code></pre> <p>This is super simple and will maintain the order of the results, but Django REST Framework does not serialize <a href="http://django-haystack.readthedocs.org/en/latest/searchqueryset_api.html#ref-searchqueryset-api" rel="nofollow"><code>SearchQuerySets</code></a>.</p> <p>Is there a method in REST Framework that I can override that would allow for the serialization of <code>SearchQuerySets</code>? Or is there a way to maintain the ranked results order without using Haystack or Python sets?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload