I am trying to make a search refiner on my website which filters keywords, the problem is that sometimes people may want to search multiple keywords. The problem is by default if they entered "hello world" it would search for that exact phrase however I want it to separate it so that it searches for "hello" and "world". So far I have added .split()
to the keywords and that did divide it however it prevented me from using __icontains
in the query. Could anyone suggest the best way to do this? Cheers!
Code -
def browse(request):
business_industry = request.GET.get('business_industry', '')
business_address_region = request.GET.get('business_address_region', '')
employment_type = request.GET.get('employment_type', '')
pay_rate = request.GET.get('pay_rate', '')
keywords = request.GET.get('keywords', '').split()
form = JobSearchForm(initial=request.GET)
filters = Q(active_listing=True)
if business_industry:
filters &= Q(business_industry=business_industry)
if business_address_region:
filters &= Q(business_address_region=business_address_region)
if employment_type:
filters &= Q(employment_type=employment_type)
if pay_rate:
filters &= Q(pay_rate=pay_rate)
if keywords:
filters &= Q(job_description__icontains=keywords) | Q(job_title__icontains=keywords)
job_listings = JobListing.objects.filter(filters).distinct().order_by('-listing_date')
context_dict = {
'joblistings': job_listings,
'form': form
}
return render(request, 'browse.html', context_dict)
Edit: I have been asked to explain why this post is unique, the other question is asking how to compare his query to all his model fields. This is asking how to filter multiple keywords from a single field.