Reputation: 305
I'm using django-haystack to power search for my application and I have boolean fields i would like to filter my models by. However, when I try doing this, my search query fails.
The search backend i'm using is elasticsearch
Upvotes: 2
Views: 2303
Reputation: 1919
sqs = sqs.filter(boolean_field=True)
did not work for me either (using Haystack 2.1.0 and Elasticsearch 1.2.1) but
sqs = sqs.filter(boolean_field=1)
did.
Upvotes: 4
Reputation: 306
Posting some of your code here would be helpful in figuring out what is going wrong for you, as it's hard to troubleshoot with nothing.
How I implemented this (using whoosh rather than elasticsearch, but the django code should be the same/similar at any rate) was like so:
created my own searchform class (if you haven't already done this, look at Haystack Docs - Creating your own form
from django import forms
from haystack.forms import SearchForm
class PaidPropertySearchForm(SearchForm):
q = forms.CharField(required=False, label=('Town Area'))
#othersearchtermshere
furnished = forms.BooleanField(required=False)
def search(self):
sqs = super(PaidPropertySearchForm, self).search()
#other search filtering here
if self.is_valid() and self.cleaned_data['furnished']:
sqs = sqs.filter(furnished__contains=self.cleaned_data['furnished'])
return sqs
relevant model field is simply:
class Properties (models.Model):.
furnished = models.BooleanField()
and my searchindexes.py:
class PropertyIndex(indexes.SearchIndex, indexes.Indexable):
#other indexed terms
furnished = indexes.BooleanField(model_attr='furnished', default='false')
Here it's worth noting that there are some issues between haystack and the whoosh backend with boolean values, that required me to specify the defult index value as a string 'false' rather than the boolean value False. If i didn't do this or didn't set a default, for some reason ALL boolean values were indexed as True, regardless of what the model had stored. I don't know if that issue would affect elasticsearch, so you may not need
default='false'
in your search index
Hopefully this helps get you on the right track if you haven't already figured it out.
Upvotes: 5