我有一个带有三个输入的表格:category
,location
和keywords
。
如果在表格中填写了所有三个字段,则结果之间的关系应为ADD
,否则只能通过location
来获取结果。
我一直在尝试将这三个输入组合在一起以进行Django全文搜索,但它仅获取与location
匹配的结果。
这是我的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
任何关于我在这里做错事情的指针都将受到高度赞赏。
我认为您的问题与上一条注释行中未定义的user_location有关。尝试这样的事情:
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