Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search functionality implementation #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions app/jobs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class RemoteType(models.TextChoices):
FULL_REMOTE = "full_remote", _("Full Remote")
CANDIDATE_CHOICE = "candidate_choice", _("Office/Remote of your choice")


class RelocateType(models.TextChoices):
NO_RELOCATE = "no_relocate", _("No relocation")
CANDIDATE_PAID = "candidate_paid", _("Covered by candidate")
Expand Down Expand Up @@ -72,13 +73,6 @@ class RemoteType(models.TextChoices):
FULL_REMOTE = "full_remote", _("Full Remote")
CANDIDATE_CHOICE = "candidate_choice", _("Office/Remote of your choice")

class CompanyType(models.TextChoices):
AGENCY = ("agency/freelance", "agency/freelance")
PRODUCT = ("product", "product")
OUTSOURCE = ("outsource/outstaff", "outsource/outstaff")
OTHER = ("other", "other")


class JobPosting(models.Model):
class Status(models.TextChoices):
"""
Expand Down
103 changes: 103 additions & 0 deletions app/jobs/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import re

from django.db.models import Q


def get_include_query(include_terms):
include_query = Q()
for term in include_terms:
escaped_term = re.escape(term)
include_query &= (
Q(position__iregex=fr'{escaped_term}') |
Q(long_description__iregex=fr'{escaped_term}') |
Q(primary_keyword__iregex=fr'{escaped_term}') |
Q(secondary_keyword__iregex=fr'{escaped_term}') |
Q(extra_keywords__iregex=fr'{escaped_term}')
)
return include_query


def get_exclude_query(exclude_terms):
exclude_query = Q()
for term in exclude_terms:
exclude_query &= (
Q(position__icontains=term) |
Q(long_description__icontains=term) |
Q(primary_keyword__icontains=term) |
Q(secondary_keyword__icontains=term) |
Q(extra_keywords__icontains=term)
)
return exclude_query


def get_full_match_rank_query(include_terms):
full_match_rank = Q()
for term in include_terms:
full_match_rank |= (
Q(position__iexact=term) |
Q(long_description__iexact=term) |
Q(primary_keyword__iexact=term) |
Q(secondary_keyword__iexact=term) |
Q(extra_keywords__iexact=term)
)
return full_match_rank


def get_partial_match_rank_query(include_terms):
partial_match_rank = Q()
for term in include_terms:
partial_match_rank |= (
(Q(position__icontains=term) & ~Q(position__iexact=term)) |
(Q(long_description__icontains=term) & ~Q(long_description__iexact=term)) |
(Q(primary_keyword__icontains=term) & ~Q(primary_keyword__iexact=term)) |
(Q(secondary_keyword__icontains=term) & ~Q(secondary_keyword__iexact=term)) |
(Q(extra_keywords__icontains=term) & ~Q(extra_keywords__iexact=term))
)
return partial_match_rank


def separate_terms(terms):
include_terms = [term for term in terms if not term.startswith('-')]
exclude_terms = [term[1:] for term in terms if term.startswith('-')]
return include_terms, exclude_terms


def apply_additional_filters(jobs, experience_years, english_level, salary_min, salary_max,
company_type, remote_type, job_domain, sort_by):

if experience_years is not None:
jobs = jobs.filter(experience_years=experience_years)

if english_level:
jobs = jobs.filter(english_level=english_level)

if salary_min is not None:
jobs = jobs.filter(salary_min__gte=salary_min)

if salary_max is not None and salary_max >= salary_min:
jobs = jobs.filter(salary_max__lte=salary_max)

if company_type:
jobs = jobs.filter(company_type=company_type)

if remote_type:
jobs = jobs.filter(remote_type__exact=remote_type)

if job_domain:
jobs = jobs.filter(domain__exact=job_domain)


# Prioritize ranking, then sort by the user selected option

if sort_by == 'date_asc':
jobs = jobs.order_by('-full_match_rank', '-partial_match_rank', 'published')
elif sort_by == 'date_desc':
jobs = jobs.order_by('-full_match_rank', '-partial_match_rank', '-published')
elif sort_by == 'reviews_asc':
jobs = jobs.order_by('-full_match_rank', '-partial_match_rank', 'reviews_count')
elif sort_by == 'reviews_desc':
jobs = jobs.order_by('-full_match_rank', '-partial_match_rank', '-reviews_count')
else:
jobs = jobs.order_by('-full_match_rank', '-partial_match_rank')

return jobs
54 changes: 50 additions & 4 deletions app/jobs/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,63 @@
from django.core.paginator import Paginator
from django.shortcuts import render
from django.db.models import Case, When, Value, IntegerField

from jobs.models import JobPosting, Company
from jobs.models import JobPosting, RemoteType, CompanyType, EnglishLevel, JobDomain

from jobs.utils import apply_additional_filters, separate_terms, get_include_query, get_exclude_query, \
get_full_match_rank_query, get_partial_match_rank_query

import random
from django.db import transaction

def jobs_list(request):
JOBS_PER_PAGE = 20

context = {
'english_level': EnglishLevel.choices,
'job_domains': JobDomain.choices,
'remote_type': RemoteType.choices,
'company_type': CompanyType.choices,
}

jobs = JobPosting.objects.all()
query = request.GET.get('q')

experience_years = request.GET.get('experience_years')
english_level = request.GET.get('english_level')
salary_min = request.GET.get('salary_min')
salary_max = request.GET.get('salary_max')
remote_type = request.GET.get('remote_type')
company_type = request.GET.get('company_type')
job_domain = request.GET.get('job_domain')

sort_by = request.GET.get('sort_by', 'date_desc') # The most recent jobs are displayed by default

if query:
terms = query.split()
include_terms, exclude_terms = separate_terms(terms)

include_query = get_include_query(include_terms)
exclude_query = get_exclude_query(exclude_terms)
full_match_rank = get_full_match_rank_query(include_terms)
partial_match_rank = get_partial_match_rank_query(include_terms)

jobs = jobs.annotate(
full_match_rank=Case(
When(full_match_rank, then=Value(2)),
default=Value(0),
output_field=IntegerField(),
),
partial_match_rank=Case(
When(partial_match_rank, then=Value(1)),
default=Value(0),
output_field=IntegerField(),
)
).filter(include_query).exclude(exclude_query)

jobs = apply_additional_filters(jobs, experience_years, english_level, salary_min, salary_max,
company_type, remote_type, job_domain, sort_by)

page = int(request.GET.get("page", 1)) or 1
paginator = Paginator(jobs, JOBS_PER_PAGE)
jobs_list = paginator.page(page)

return render(request, "jobs/list.html", { "jobs": jobs_list })
return render(request, 'jobs/list.html', {'jobs': jobs_list, **context})
Loading