Combine the results of the two Haystack querysets

I have two sets of requests that I receive doing the following:

q = Q(is_visible='true')
q |= Q(user='some_user')
q &= Q(text=request.GET.get('query', ''))

sqs1 = SearchQuerySet().filter(q)

sqs2 = SearchQuerySet().models(model.Some).filter(text=request.GET.get('query', ''))

      

If I just combine these two sets of requests, I don't get what I want. As the items will be evaluated on their specific request. I want to somehow combine these two queries before passing them to the SearchQuerySet so that the search database calculates the scores for this entire set.

I thought about putting the second query in the Q chain of the first query somehow, but I couldn't figure out how to search only based on the model.

+3


source to share


1 answer


So, I solved this problem using the following query:

request_text = request.GET.get('query', '')
user = request.user
sqs = SearchQuerySet().filter(text=request_text).exclude(~Q(user='some_user') | Q(is_visible='false')

      



It seems that it is possible to rephrase the problem with a filter and exclude.

+2


source







All Articles