Skip to content

Commit

Permalink
implemented search
Browse files Browse the repository at this point in the history
  • Loading branch information
mhk19 committed Apr 10, 2020
1 parent cbc58f0 commit 8f9eae8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions rest_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@
url(r'^filerequests', views.FileRequestViewSet.as_view()),
url(r'^courserequests', views.CourseRequestViewSet.as_view()),
url(r'^uploads', views.UploadViewSet.as_view()),
url(r'^search', views.SearchViewSet.as_view()),
]
51 changes: 51 additions & 0 deletions rest_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import base64
import jwt
import os
import itertools
from rest_api.documents import CourseDocument, DepartmentDocument, FileDocument

NEXUS_URL = "http://nexus.sdslabs.local/api/v1"

Expand Down Expand Up @@ -71,6 +73,7 @@ def get_extra_actions(cls):
class CourseViewSet(APIView):
def get(self, request):
queryset = Course.objects.all()
print(type(queryset))
department = self.request.query_params.get('department')
course = self.request.query_params.get('course')
if department is not None and course == 'null':
Expand Down Expand Up @@ -463,3 +466,51 @@ def post(self, request):
@classmethod
def get_extra_actions(cls):
return []


class SearchViewSet(APIView):
def get(self,request):
q = request.query_params.get('q')

if q:
courses = CourseDocument.search().query("multi_match", query=q, type="phrase_prefix", fields=['title','code'])
departments = DepartmentDocument.search().query("multi_match", query=q, type="phrase_prefix", fields=['title','abbreviation'])
files = FileDocument.search().query("multi_match", query=q, type="phrase_prefix", fields=['title','fileext','filetype'])

response_courses = courses.execute()
queryset_courses = Course.objects.none()
response_departments = departments.execute()
queryset_departments = Department.objects.none()
response_files = files.execute()
queryset_files = File.objects.none()

for hit in response_files.hits.hits:
fileId = hit['_source']["id"]
query_files = File.objects.filter(id=fileId)
queryset_files = list(itertools.chain(queryset_files,query_files))

for hit in response_departments.hits.hits:
departmentId = hit['_source']["id"]
query_departments = Department.objects.filter(id=departmentId)
queryset_departments = list(itertools.chain(queryset_departments,query_departments))

for hit in response_courses.hits.hits:
courseId = hit['_source']["id"]
query = Course.objects.filter(id=courseId)
queryset_courses = list(itertools.chain(queryset_courses,query))

serializer_courses = CourseSerializer(queryset_courses,many=True).data
serializer_departments = DepartmentSerializer(queryset_departments,many=True).data
serializer_files = FileSerializer(queryset_files,many=True).data

return Response({
"departments" : serializer_departments,
"courses" : serializer_courses,
"files" : serializer_files,
})
else:
return Response("search query not found")

@classmethod
def get_extra_actions(cls):
return []
7 changes: 7 additions & 0 deletions studyportal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
'rest_api',
'rest_framework',
'corsheaders',
'django_elasticsearch_dsl',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -144,3 +145,9 @@
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'

ELASTICSEARCH_DSL={
'default':{
'hosts': 'localhost:9200'
}
}

0 comments on commit 8f9eae8

Please sign in to comment.