I have a form with three inputs: category, location, and keywords.
If all three fields are filled in the form, the relationship of the results should be ADD, otherwise will only fetch results by location.
I have been trying to combine these three inputs for Django full-text search but it fetches results matching the location only.
Here is my code for views.py:
class SearchServices(generic.ListView):
model = Service
context_object_name = 'services'
template_name = 'services/search.html'
def get_queryset(self):
qs = Service.objects
if self.request.GET['location'] != '':
lat_long = self.request.GET['location'].split(',')
user_location = Point(float(lat_long[1]), float(lat_long[0]), srid=4326)
keywords = self.request.GET['keywords']
category = self.request.GET['category']
query = SearchQuery(keywords) & SearchQuery(category)
vector = SearchVector('title', 'description', StringAgg('tag__name', delimiter=' ')) + SearchVector(StringAgg('category__slug', delimiter=' '))
if self.request.GET['location'] != '':
qs = qs.filter(location__distance_lte=(user_location, D(km=2)))
if category != '':
qs = qs.annotate(search=vector).filter(search=query).distinct()
qs = qs.annotate(rank=SearchRank(vector, query)).order_by('-rank')
qs = qs.annotate(distance=Distance('location', user_location)).order_by('distance')
return qs
Any pointers as to what I am doing wrong here would be highly appreciated.