2

Is there any way to get an union of two or more searchqueryset ? For example :-

query1 = SearchQuerySet().filter(authors=query)
query2 = SearchQuerySet().filter(title=query)

How do I combine both the queries together so that i get a union of the 2 queries ?

falsetru
  • 357,413
  • 63
  • 732
  • 636
Subhajit
  • 361
  • 1
  • 4
  • 18

1 Answers1

4

SearchQuerySet implements the QuerySet interface to some extent, so you can try:

query1 | query2
Hunan Rostomyan
  • 2,176
  • 2
  • 22
  • 31
  • As it is built on QuerySet then why cant we apply the `prefetch_related` attribute? – Subhajit Dec 05 '15 at 06:09
  • Good question. I'm not sure what the answer is. You can try `filter_and` instead. – Hunan Rostomyan Dec 05 '15 at 06:10
  • This is my exact doubt -> http://stackoverflow.com/questions/34060293/which-attribute-of-searchqueryset-has-the-same-function-as-prefetch-related can you please give an idea how to use filter_and in it – Subhajit Dec 05 '15 at 06:23
  • @Subhajit [Here, in the docs](https://django-haystack.readthedocs.org/en/v2.4.1/searchqueryset_api.html?highlight=#why-follow-queryset) they admit that the affinity to `QuerySet` is partial. I'll adjust my answer to reflect that. – Hunan Rostomyan Dec 05 '15 at 06:25