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.
I think your problem has to do with an undefined user_location in your last annotate line. Try something like this:
class SearchServices(generic.ListView):
model = Service
context_object_name = 'services'
template_name = 'services/search.html'
def get_queryset(self):
qs = Service.objects.all()
request_location = self.request.GET.get('location', '')
request_keywords = self.request.GET.get('keywords', '')
request_category = self.request.GET.get('category', '')
query = SearchQuery(request_keywords) & SearchQuery(request_category)
vector = SearchVector(
'title',
'description',
StringAgg('tag__name', delimiter=' ')
) + SearchVector(
StringAgg('category__slug', delimiter=' ')
)
if request_category != '':
qs = qs.annotate(search=vector).filter(search=query).distinct()
qs = qs.annotate(rank=SearchRank(vector, query)).order_by('-rank')
if len(request_location.split(',')) == 2:
lat_long = request_location.split(',')
user_location = Point(float(lat_long[1]), float(lat_long[0]), srid=4326)
qs = qs.filter(location__distance_lte=(user_location, D(km=2)))
qs = qs.annotate(
distance=Distance('location', user_location)
).order_by('distance')
return qs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.