Skip to content
Draft
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
58 changes: 58 additions & 0 deletions backend/climateconnect_api/utility/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from concurrent.futures import ThreadPoolExecutor
from climateconnect_api.models.user import UserProfile

from organization.models import Organization
from organization.models import Project
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
from django.db.models import Value



def _search_project(input_text:str):
#https://docs.djangoproject.com/en/4.1/ref/contrib/postgres/search/
vector = SearchVector('description','short_description','name')
search_query = SearchQuery(input_text)
data = Project.objects.annotate(rank=SearchRank(vector, search_query, cover_density=True)).order_by('-rank')[:10]
serialize_data = lambda p:{"name":p.name, "description":p.description if len(p.short_description)==0 else p.short_description,"url":p.url_slug, "short":p.short_description,"rank":p.rank }
return list(map(serialize_data,data))

def _search_profile(input_text:str):
#https://docs.djangoproject.com/en/4.1/ref/contrib/postgres/search/
vector = SearchVector('skills','name')
search_query = SearchQuery(input_text)
data = UserProfile.objects.annotate(rank=SearchRank(vector, search_query, cover_density=True)).order_by('-rank')[:10]
serialize_data = lambda p:{"name":p.name ,"url":p.url_slug, "rank":p.rank }
return list(map(serialize_data,data))

def _search_organization(input_text:str):
#https://docs.djangoproject.com/en/4.1/ref/contrib/postgres/search/
vector = SearchVector('about','name','short_description','website')
search_query = SearchQuery(input_text)
data = Organization.objects.annotate(rank=SearchRank(vector, search_query, cover_density=True)).order_by('-rank')[:10]
serialize_data = lambda p:{"name":p.name, "short_description":p.short_description,"website":p.website,"about":p.about, "url":p.url_slug ,"rank":p.rank }
return list(map(serialize_data,data))


def cross_search(input_text:str):
#_profiles = []
_projects = []
_organizations = []
try:
with ThreadPoolExecutor(max_workers=3) as tpe:
threads = {
"projects": tpe.submit(_search_project,input_text),
"organizations": tpe.submit(_search_organization,input_text),
#"profiles": tpe.submit(_search_profile,input_text),
}

results = {#"profiles":threads['profiles'].result()
"projects":threads['projects'].result()
, "organizations":threads['organizations'].result()}

print(results)

except Exception as E:
raise Exception(E)

return results

31 changes: 31 additions & 0 deletions backend/climateconnect_api/views/search_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.utils.translation import get_language
from climateconnect_api.utility.translation import translate_text
from django.conf import settings
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.exceptions import ValidationError

from climateconnect_api.utility.search import cross_search


class CrossSearchView(APIView):
permission_classes = (AllowAny,)

def post(self, request):
required_params = ["text"]
for param in required_params:
if param not in request.data:
raise ValidationError("Required parameter missing: " + param)


results = cross_search(input_text=request.data["text"])

print(results)

return Response({#"profiles": results["profiles"],
"projects": results["projects"],
"organizations" : results["organizations"]
}
, status=status.HTTP_200_OK)
6 changes: 6 additions & 0 deletions backend/climateconnect_main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
notifications_views,
donation_views,
translation_views,
search_view
)
from knox import views as knox_views
from django.conf import settings
Expand Down Expand Up @@ -162,6 +163,11 @@
badge_views.getDonorBadges.as_view(),
name="get-possible-donor-badges",
),
path(
"api/cross_search",
search_view.CrossSearchView.as_view(),
name="cross-search-objects",
),
# Organization views
path("api/", include("organization.urls")),
# Chat messages views
Expand Down