Tuesday, May 3, 2011

How to intersect SphinxQuerySet with QuerySet

Is it possible to intersect 2 querysets: SphinxQuerySet and normal django's QuerySet ?

I need to filter SphynxQuerySet by ordinary django's filters. What the best way to do that?

I try go this direction:

ids = []
for obj in Object.search.query(query).all():
        ids += [obj.id]
qs = Object.objects.all().filter(id__in=ids).filter(some_other_filters)

But I feel that's not best method for my purpose.

From stackoverflow
  • At one point there was a (now fixed) bug in django-sphinx where filters from previous queries weren't being cleared, which meant I had to do full search without any filters and then intersect with normal filters. This is how I did it:

    # First get list of objects matching criteria
    results = Object.objects.filter(qset).distinct()
    
    # Save id's from mysql queryset for later
    qs_ids = [object.id for object in results]
    
    # Do sphinx query
    search_results = Object.search.query(search_terms)
    
    # Now manually filter the sphinx queryset
    results = [object for object in search_results if object.id in qs_ids]
    
    return results
    

    This isn't as efficient as letting sphinx do the filtering of course, but I didn't have a choice at the time.

    Maybe you can re-think your strategy so you don't need to manually filter the query yourself?

    ramusus : thanks for more convenient syntax! Do you change built-in hardcoded limit of SphynxQuerySet self._limit = 20 to greater value? I didn't find method to do this through standart interface.
    Van Gale : I actually like your use of id__in=ids better than my solution, but there was some reason I couldn't do that... lost to the mists of time :) I don't think I did anything with self._limit, but that's probably because the site never went live :)

0 comments:

Post a Comment