At 12:35 PM

#Django-haystack - #ElasticSearch (and probably #Solr) - how to filter/exclude by None/Null values value

If you are using django-haystack, you may notice that filter by None won’t work:

from haystack.query import SearchQuerySet

Haystack won’t report any error and will return no results.

Workaround for this I found by using Raw input field type value. So, solution follows:

from haystack.inputs import Raw
SearchQuerySet().exclude(some_field=Raw("[* TO *]")})

Other way around, filter all documents that have non-emtpy field value:

from haystack.inputs import Raw
SearchQuerySet().filter(some_field=Raw("[* TO *]")})

This could work for SOLR too and for other Haystack backends one can apply the same concept but with specific syntax of backend search language.

This solution is posted to StackOverflow where one can find more details:

blog comments powered by Disqus