Boolean operators

177 views
Skip to first unread message

Kegan Gan

unread,
Nov 30, 2009, 12:14:16 AM11/30/09
to django-haystack
How to use boolean operators using haystack search? Can't seems to
find in the documentation.

For example for Whoosh, one can search for:

. "term1 OR term 2" ........... return results that contains term1 or
term2.
. "term1 AND term 2" ........... return results that contains both
term1 and term2.

Thanks.

@@

unread,
Nov 30, 2009, 1:32:42 AM11/30/09
to django-...@googlegroups.com
There's a filter_or and filter_and
for example:



--

You received this message because you are subscribed to the Google Groups "django-haystack" group.
To post to this group, send email to django-...@googlegroups.com.
To unsubscribe from this group, send email to django-haysta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-haystack?hl=en.



Daniel Lindsley

unread,
Dec 1, 2009, 1:13:37 AM12/1/09
to django-...@googlegroups.com
Kegan,


There are two ways to do this, depending on how complex the
task/query is. The simple approach is:

from haystack.query import SearchQuerySet
sqs = SearchQuerySet().filter(content='hello').filter_or(content='world')
# Query becomes: "hello OR world"

However, that only works for simple cases and you need to be
relatively careful how you chain. The advanced approach is to do:

from haystack.query import SearchQuerySet, SQ
sqs = SearchQuerySet().filter(SQ(content='hello') | SQ(content='world'))
# Query becomes: "(hello OR world)"

The second variant allows for arbitrarily complex queries with lots
of nesting and grouping. I'll work on better documenting ``SQ`` in the
future.


Daniel
Reply all
Reply to author
Forward
0 new messages