haystack2: Combining queries #613

Closed
mjl opened this Issue Jul 18, 2012 · 1 comment

Projects

None yet

2 participants

@mjl
mjl commented Jul 18, 2012

Here's the simple version, that works fine:

>>> from haystack.query import SearchQuerySet
>>> sqs = SearchQuerySet()
>>> q1 = sqs.filter(no_restriction=True)
>>> q2 = sqs.filter(no_restriction=False)
>>> len(q1)
760
>>> len(q2)
200
>>> len(q1 | q2)
960
>>> len(q2 | q1)
960

And here the same thing, just with a base query more:

>>> sqs = SearchQuerySet().filter(result_type='product')
>>> len(sqs)
465
>>> q1 = sqs.filter(no_restriction=True)
>>> q2 = sqs.filter(no_restriction=False)
>>> len(q1)
289
>>> len(q2)
176
>>> len(q1 | q2)
176
>>> len(q2 | q1)
289

So it seems that chained filters and combining queries is totally broken.

I suspect this is the place to correct that problem:

--- a/haystack/backends/__init__.py
+++ b/haystack/backends/__init__.py
@@ -599,7 +599,7 @@ class BaseSearchQuery(object):
         else:
             connector = SQ.AND

-        if self.query_filter and query_filter.connector != SQ.AND and len(query_filter) > 1:
+        if self.query_filter and query_filter.connector != connector and len(query_filter) > 1:
             self.query_filter.start_subtree(connector)
             subtree = True
         else:
@mjl
mjl commented Jul 24, 2012

Added test cases for the issue plus the patch above in 9192dbd

@acdha acdha added a commit that closed this issue Nov 25, 2012
@acdha acdha Fix combination logic for complex queries
Previously combining querysets which used a mix of logical AND and OR operations
behaved unexpectedly.

Thanks to @mjl for the patch and tests in SHA: 9192dbd

Closes #613, #617
907bdee
@acdha acdha closed this in 907bdee Nov 25, 2012
@floppya floppya added a commit to floppya/django-haystack that referenced this issue Mar 29, 2013
@acdha acdha Fix combination logic for complex queries
Previously combining querysets which used a mix of logical AND and OR operations
behaved unexpectedly.

Thanks to @mjl for the patch and tests in SHA: 9192dbd

Closes #613, #617
749b72d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment