I'm aware of full text search applications like Django Solr and solango, etc.
What I'm looking to built is more akin to an advanced search for a real estate site. E.g they can choose location, price, etc. similar to www.viewr.com advanced search.
What I have done so far is this, under the models custom manager:
def advanced_search(self, district, location, type, facilities, features, not_permitted):
q_objects = []
l_objects = []
t_objects = []
fc_objects = []
ft_objects = []
np_objects = []
if district:
if location:
for loc in location:
l_objects.append(Q(location__exact=loc))
else:
l_objects.append(Q(location__district=district))
if type:
for ty in type:
t_objects.append(Q(listing_type__exact=ty))
if facilities:
for fc in facilities:
fc_objects.append(Q(property_facilities__exact=fc))
if features:
for ft in features:
ft_objects.append(Q(property_features__exact=ft))
ft_objects.append(Q(community_features__exact=ft))
if not_permitted:
for np in not_permitted:
np_objects.append(Q(not_permitted__exact=np))
# Start with a bare QuerySet
qs = self.get_query_set()
if location:
qs = qs.filter(reduce(operator.or_, l_objects))
if type:
qs = qs.filter(reduce(operator.or_, t_objects))
if facilities:
qs = qs.filter(reduce(operator.or_, fc_objects))
if features:
qs = qs.filter(reduce(operator.or_, ft_objects))
if not_permitted:
qs = qs.filter(reduce(operator.or_, np_objects))
# Use operator's or_ to string together all of your Q objects.
return qs
Right now I'm not getting very predictable results. Is there something I might be doing wrong? Is there a better way of doing the various OR searches/joins?